Shortcode doesn’t work if I directly paste its function in a template file?

This is a shortcode to add a question form in the Question and Answer Forum plugin for WordPress:

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

This short code calls this:

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

and this is 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;

I created a template file called, question-form-page.php, I pasted the whole code of askquestionform.php but nothing is displayed (blank).

I also tried placing the following in question-form-page.php:

question_form($atts,$content = "")

and

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

But I get the same result.

Any suggestions?

Related posts

Leave a Reply

3 comments

  1. As dalton wrote, you are looking for the shortcode function. You can use the shortcode in your php files via this function:

    Try to put this lines in your question-form-page.php file:

    <? echo do_shortcode('[question_form width='x' bordercolor='x' style='x']question form title[/question_form]'); ?>
    
  2. First test on a normal post whether the shortcode actually works. If it does, use this on your template to run it

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