Below, you’ll find a detailed guide on how to add the AnsPress – Question and answer Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the AnsPress – Question and answer Plugin shortcodes not to show or not to work correctly.
Before starting, here is an overview of the AnsPress – Question and answer Plugin and the shortcodes it provides:
"AnsPress – Question and Answer is a dynamic WordPress plugin perfect for creating interactive Q&A forums on your site. With this, engage your audience in thoughtful discussions."
- [apcode]
- [anspress]
- [question]
AnsPress [apcode] Shortcode
The AnsPress Question-Answer plugin shortcode is a tool that formats the display of code snippets. It uses the ‘apcode’ shortcode to parse the attributes and content. It allows customization of language and inline display. The content is stripped of break tags and class attributes are added based on the language selected. It replaces the ‘
Shortcode:
[apcode]
Parameters
Here is a list of all possible apcode shortcode parameters and attributes:
language
– Specifies the programming language for syntax highlighting.inline
– Determines whether the code is displayed inline or as a block.
Examples and Usage
Basic example – The following shortcode is a simple usage of the ‘apcode’ shortcode. It applies the default ‘plain’ language setting and does not apply the ‘inline’ attribute.
[apcode /]
Advanced examples
Here, we use the ‘apcode’ shortcode to specify the language attribute. This example sets the language to ‘php’, which might be useful when displaying code snippets in a PHP context. The ‘inline’ attribute is also set to ‘true’, which means the code will be displayed in an inline format.
[apcode language="php" inline="true" /]
In this next advanced example, we use the ‘apcode’ shortcode with the ‘language’ attribute set to ‘javascript’ and the ‘inline’ attribute set to ‘false’. This could be useful for displaying JavaScript code blocks in a non-inline format.
[apcode language="javascript" inline="false" /]
PHP Function Code
In case you have difficulties debugging what causing issues with [apcode]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'apcode', array( $this, 'shortcode' ) );
Shortcode PHP function:
function shortcode( $atts, $content = '' ) {
$atts = wp_parse_args(
$atts,
array(
'language' => 'plain',
'inline' => false,
)
);
$tag = $atts['inline'] ? 'code' : 'pre';
$content = preg_replace( '/<br(\s+)?\/?>/i', '', $content );
$klass = 'class="brush: ' . esc_attr( $atts['language'] ) . '"';
$content = str_replace( array( '<pre', '<code' ), array( '<pre ' . $klass, '<code ' . $klass ), $content );
return $content;
}
Code file location:
anspress-question-answer/anspress-question-answer/addons/syntaxhighlighter/syntaxhighlighter.php
AnsPress [anspress] Shortcode
The AnsPress BasePage shortcode is a versatile tool for WordPress. It enables the AnsPress Question-Answer plugin to function on a page, allowing users to ask and answer questions. This shortcode includes important scripts and styles necessary for the plugin’s operation. It also prevents nesting of the shortcode and provides a link back to the author, if required.
Shortcode: [anspress]
Examples and Usage
Basic example – The AnsPress shortcode is utilized to display the AnsPress question and answer section on a page.
[anspress /]
Advanced examples
Utilizing the AnsPress shortcode with additional parameters to customize the output. In this example, we’re specifying a page attribute to define which AnsPress page should be displayed.
[anspress page="questions" /]
Another example of using the AnsPress shortcode with multiple parameters. Here, we’re specifying both a page and an order_by attribute to customize the output. The page attribute defines which AnsPress page should be displayed, and the order_by attribute determines the order in which the questions are listed.
[anspress page="questions" order_by="votes" /]
Please note that the actual parameters available for the AnsPress shortcode may vary depending on the version of the AnsPress plugin you are using, and the specific configuration of your WordPress site. Always refer to the AnsPress documentation for the most accurate and up-to-date information.
PHP Function Code
In case you have difficulties debugging what causing issues with [anspress]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'anspress', array( AnsPress_BasePage_Shortcode::get_instance(), 'anspress_sc' ) );
Shortcode PHP function:
function anspress_sc( $atts, $content = '' ) {
global $ap_shortcode_loaded;
// Drop current page variable cache. As we are allowing to change page from shortcode.
wp_cache_delete( 'current_page', 'anspress' );
// Check if AnsPress shortcode already loaded.
if ( true === $ap_shortcode_loaded ) {
return __( 'AnsPress shortcode cannot be nested.', 'anspress-question-answer' );
}
wp_enqueue_script( 'anspress-main' );
wp_enqueue_script( 'anspress-theme' );
wp_enqueue_style( 'anspress-main' );
wp_enqueue_style( 'anspress-fonts' );
$ap_shortcode_loaded = true;
$this->attributes( $atts, $content );
ob_start();
echo '<div id="anspress" class="anspress">';
/**
* Action is fired before loading AnsPress body.
*/
do_action( 'ap_before' );
// Include theme file.
ap_page( $this->current_page );
echo '</div>';
// Linkback to author.
if ( ! ap_opt( 'author_credits' ) ) {
echo '<div class="ap-cradit">' . esc_attr__( 'Question and answer is powered by', 'anspress-question-answer' ) . ' <a href="https://anspress.net" target="_blank">anspress.net</a></div>';
}
wp_reset_postdata();
$ap_shortcode_loaded = false;
return ob_get_clean();
}
Code file location:
anspress-question-answer/anspress-question-answer/includes/class-theme.php
AnsPress [question] Shortcode
The AnsPress Question Shortcode is designed to display a specific question on a webpage. It fetches the question using the provided ID within the shortcode. The shortcode starts by setting up a buffer and creating a div for the question. It then checks if the ID provided is valid and fetches the corresponding question. If the question exists, it is displayed using the question template. If not, an error message is shown. After displaying the question or error message, the post data is reset and the buffered content is returned.
Shortcode: [question]
Parameters
Here is a list of all possible question shortcode parameters and attributes:
ID
– The unique identifier for the question.
Examples and Usage
Basic example – The shortcode displays a specific question from the AnsPress Question Answer plugin using the question’s ID.
[question id=1 /]
Advanced examples
Using the shortcode to display a specific question by referencing the ID. If the question with the given ID does not exist, an error message will be displayed.
[question ID=5 /]
It’s also possible to use lowercase ‘id’ in the shortcode. The functionality remains the same, it will display the question with the specified id or an error message if the question does not exist.
[question id=5 /]
Please note that the ‘id’ in the shortcode refers to the ID of the question in the AnsPress Question Answer plugin, not the post ID of the page or post where the shortcode is used.
PHP Function Code
In case you have difficulties debugging what causing issues with [question]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'question', array( AnsPress_Question_Shortcode::get_instance(), 'anspress_question_sc' ) );
Shortcode PHP function:
function anspress_question_sc( $atts, $content = '' ) {
ob_start();
echo '<div id="anspress" class="ap-eq">';
/**
* Action is fired before loading AnsPress body.
*/
do_action( 'ap_before_question_shortcode' );
$id = ! empty( $atts['ID'] ) ? absint( $atts['ID'] ) : absint( $atts['id'] );
$questions = ap_get_question( $id );
if ( $questions->have_posts() ) {
/**
* Set current question as global post
*
* @since 2.3.3
*/
while ( $questions->have_posts() ) :
$questions->the_post();
include ap_get_theme_location( 'shortcode/question.php' );
endwhile;
} else {
esc_attr_e( 'Invalid or non existing question id.', 'anspress-question-answer' );
}
echo '</div>';
wp_reset_postdata();
return ob_get_clean();
}
Code file location:
anspress-question-answer/anspress-question-answer/includes/class-theme.php
Conclusion
Now that you’ve learned how to embed the AnsPress – Question and answer Plugin shortcodes, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.
Leave a Reply