web-dev-qa-db-fra.com

Le shortcode ne fonctionne pas si je colle directement sa fonction dans un fichier modèle?

Ceci est un shortcode pour ajouter un formulaire de question dans le plugin Forum de questions et réponses pour Wordpress:

[question_form width=\'x\' bordercolor=\'x\' style=\'x\']question form title[/question_form]

Ce code abrégé appelle ceci:

//allow short codes  [question_form]
function question_form($atts,$content = "")
{
    global $questionformid;
    include "askquestionform.php";
    return $html;
}
add_shortcode('question_form','question_form');

et c'est askquestionform.php:

<?php
load_plugin_textdomain( 'qna-forum', false, 'question-and-answer-forum/languages' );
if(get_option('q_loginrequired_ask')=="TRUE" && !is_user_logged_in())
{
    $html = "<p>".__("Please login to ask a question",'qna-forum')."</p>";
    $html .= q_loginform(get_permalink());
    return $html;
}

                                        //STYLING INFORMATION=============
$formstyle = "";
$introstyle = "";
if(isset($atts['width']))
{
    $formstyle .= "width:" . $atts['width'] . ";";
}
if(isset($atts['style']))
{
    $formstyle .= $atts['style'];
}
if(isset($atts['bordercolor']))
{
    $formstyle .= "border: 2px solid " . $atts['bordercolor'] . ";";
    $introstyle .= "background-color:" . $atts['bordercolor'] . ";";
}

if(!isset($_POST['title']) || $_POST['title'] == null)
{
    $qtitle = __("Enter Question Title",'qna-forum');
}
else{
    $qtitle = $_POST['title'];
}
if(!isset($_POST['question']) || $_POST['question'] == null)
{
    $Qtext = __("Enter Your Question Here",'qna-forum');
}
else{
    $Qtext = $_POST['question'];
}

$html = '<form class="questionform" name="questionform-'. $questionformid . '" id="questionform-' . $questionformid. '" style=\'' . $formstyle. '\'>

<div class="question-form-intro" style="' . $introstyle . '">';
if($content != "")
{
    $html = $html . $content;
}
else
{
    $html = $html . __("Use the form below to ask a question",'qna-forum');
}
$html = $html . '</div>';
include_once (ABSPATH . 'wp-content/plugins/question-and-answer-forum/coreform.php');
$html = $html . '</form>';

$questionformid = $questionformid + 1;

J'ai créé un fichier modèle appelé question-form-page.php , j'ai collé tout le code de askquestionform.php mais rien est affiché (vide).

J'ai également essayé de placer les éléments suivants dans question-form-page.php :

question_form($atts,$content = "")

et

global $questionformid;
include "askquestionform.php";
return $html;

Mais j'obtiens le même résultat.

Aucune suggestion?

1
janoChen

Je pense que vous recherchez la fonction do_shortcode(). Il vous permet de coller un shortcode directement dans un fichier de modèle.

http://codex.wordpress.org/Function_Reference/do_shortcode

4
Dalton

Premier test sur un article normal pour savoir si le shortcode fonctionne réellement. Si c'est le cas, utilisez ceci sur votre modèle pour l'exécuter

<?php echo do_shortcode('[shortcode]'); ?>
1
lightyoruichi

Comme Dalton l’a écrit, vous recherchez la fonction shortcode. Vous pouvez utiliser le shortcode dans vos fichiers php via cette fonction:

Essayez de mettre ces lignes dans votre fichier question-form-page.php :

<? echo do_shortcode('[question_form width=\'x\' bordercolor=\'x\' style=\'x\']question form title[/question_form]'); ?>
1
keatch