WordPress : Ajout de code PHP/HTML à l’intérieur de la page à partir d’un modèle personnalisé

Publié par Jean-Michel le

J’aide mon fils avec son site financier avec une programmation personnalisée. Le premier projet pour lui est une feuille de calcul personnalisée utilisant jQuery, flot, php, etc.

J’ai créé un thème enfant, créé un modèle, functions.php, une feuille de style, etc., qui fonctionnent bien – tant que la feuille de calcul est à la fin de la page. Mais maintenant, j’essaie de l’améliorer en ajoutant la possibilité pour lui d’ajouter du texte avant et après la feuille de calcul.

My first idea was that I might be able to simply use a shortcode, but I don’t know if that’s the correct way. It seems that shortcodes are not for outputting large amounts of mixed php/html. I can include the file and call a function in the spreadsheet file, and that works, but the shortcode so far hasn’t done anything except print out the actual shortcode itself. But if it’s an incorrect method, I don’t want to waste time trying to make it work.

Any ideas for how I can do this? I suppose I could just manually parse the_post() and process a shortcode myself, but I’d hate to do that if there’s a method I can use that’s built in.

More Info
I tried experimenting with shortcodes. In my functions.php (which I know is being used because I load some javascript and css files):

function register_shortcodes() {
  add_shortcode('spreadsheet_placeholder', 'display_spreadsheet');
}
add_action( 'init', 'register_shortcodes');

I put a tag [spreadsheet_placeholder] in the middle of a page, and the page rendered with that tag in the middle. Up until today, it just displayed the tag, but just now I got the bright idea to include the external php file containing the function (display_spreadsheet).

I got the spreadsheet to display after including the php file, just not the way I wanted. But it did work. It just displayed above the content rather than in the middle. I had read that I need to return the data, not just ouput it directly, so I expected something like that.

I had tried using a nowdoc variable to save the entire function to a variable, but I think this is overly optimistic (maybe very much so), as I have mixed php and html with php tags, so I guess I would need to go into the php code and save it as several variables and concatenate them together to return the entire code. But that’s more of a php problem than WordPress, I guess.

At this point, I’m not sure which is easier – doing that, or parsing for a shortcode-like tag myself in the content of the page, and doing the logic in the code.

The solution

Merci à bonger et Brad Dalton pour leur aide avec l’utilisation de ob_start(), quelque chose que je n’ai jamais utilisé auparavant.

J’ai sorti mon code php/html d’origine de son fichier et l’ai placé dans un fichier séparé, à l’intérieur d’une fonction. J’ai inclus ce fichier dans functions.php et pointé l’enregistrement du shortcode vers la fonction. Ensuite, en utilisant l’exemple de Brad, j’ai renvoyé le fichier entier à la fin et tout a parfaitement fonctionné.

Merci à vous deux. Je suis content du résultat et mon fils aussi.

Solution n°1 trouvée

Voici comment coder le shortcode

function foobar_func( $atts ){
    return "foo and bar";
}
add_shortcode( 'foobar', 'foobar_func' );

Et pour exécuter une fonction en utilisant la mise en mémoire tampon de sortie

add_shortcode( 'shortcode_tag', 'function_name' );
function function_name($atts) {
    ob_start();

    // Add your code

    $var = ob_get_contents();
    ob_end_clean();
    return $var;

}

Catégories : Wordpress

Jean-Michel

Jean-Michel est en charge de la partie blog du site. Il met en place la stratégie de contenu et répond aux questions fréquentes sur Wordpress.

0 commentaire

Laisser un commentaire

Avatar placeholder

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *