I am working on wordpress shortcode. In the functions.php, I have added following code
add_shortcode('post-footer-note', 'getPostFooterNote');
function getPostFooterNote() {
return "<div>Hope you find this tutorial useful! <a href='#'>Share this link</a> on Social Media.</div>";
}
and in the post page, I added [post-footer-note] The code works fine. Instead of "Hope you find this tutorial useful" text I want to display WooCommerce products. To show the products on the page, I made few changes in the code
add_shortcode('post-footer-note', 'getPostFooterNote');
function getPostFooterNote() {
return "<div>[products limit="8" columns="4" category="hoodies, tshirts" cat_operator="NOT IN"]</div>";
}
After making changes, following error is showing.
Your PHP code changes were rolled back due to an error on line 461 of file C:\xampp\htdocs\wordpress\wp-content\themes\twentynineteen\functions.php. Please fix and try saving again.
syntax error, unexpected integer "8", expecting ";"
You have a syntax error in:
function getPostFooterNote() {
return "<div>[products limit="8" columns="4" category="hoodies, tshirts"
cat_operator="NOT IN"]</div>";
}
You need to use different quotations like this:
function getPostFooterNote() {
return do_shortcode("<div>[products limit='8' columns='4' category='hoodies, tshirts' cat_operator='NOT IN']</div>");
}
You could also flip the quotes (it doesn't matter which is the outer quotation marks (double or single).
As per the error, the second double quote is just before the 8, and therefore the return is concluded, and a semicolon is expected. Instead 8 is next, and the compiler doesn't know how to parse your code.
Edit: added #Moshe Gross' edit to final solution.
Related
I am trying to insert this code to > Elementor > Custom Code to change the Sale Badge Text in WooCommerce
add_filter(‘woocommerce_sale_flash’, ‘edit_sale_badge’); function edit_sale_badge() { return ‘TEXT-GOES-HERE’; }
From this page: https://pluginsforwp.com/blog/change-sale-badge-woocommerce/ – But I dont know how to make it work, can anyone help me? Can PHP be added to Elementor custom code? Thanks!
wrap the code with th PHP but getting the error: php special characters must be escaped
Did you wrap your code with
<?php and ?>
So that it looks like the following:
<?php
add_filter('woocommerce_sale_flash', 'edit_sale_badge');
function edit_sale_badge() {
return 'TEXT-GOES-HERE';
}
?>
In your Link, they add it to the Themes Functions:
functions.php
Why not do it also there?
Note: there is a difference between the following symbols `, ´ and '. You want to use the last one.
You cant insert php in Elementor Custom Code area.. You must add this via functions.php in your child theme. Or you can add this via other plugins (2).
Look also here: https://elementor.com/help/custom-code-pro/
(2): https://de.wordpress.org/plugins/insert-php-code-snippet/
WordPress Divi Theme shortcode adding problem
My Divi version is 3.20.1. I am trying to add my own custom shortcode in the website. However, when I add this shortcode, the elements displayed using this shortcode appear both in the "Edit page" area top section besides the main page.
add_shortcode( "Btx_Show_Testimonial_Main_Page", 'lantry_btx_fun_Main_Page_Show_Testimonial');
function lantry_btx_fun_Main_Page_Show_Testimonial(){
include_once LANTRY_BITECHX_SHORTCODE_DIR_PATH."views/Main_Page_testimonial_show.php";
}
My question is, how can I remove this from the "Edit page" section ??
I provided some screenshot.
Divi module Image Option Select
Show top of the post page
When I remove Divi this problem will be solved. But I need to use Divi.
You can check if a adminuser is logged in and if not execute your function hope this helps but now you have to always log out or use incognito mode to see if its there:
add_shortcode( "Btx_Show_Testimonial_Main_Page", 'lantry_btx_fun_Main_Page_Show_Testimonial');
function lantry_btx_fun_Main_Page_Show_Testimonial(){
if (current_user_can( 'update_core' )) {
return;
}
include_once LANTRY_BITECHX_SHORTCODE_DIR_PATH."views/Main_Page_testimonial_show.php";
}
I don't think the problem comes from DIVI but from your shortcode. Please attach the content of your file Main_Page_testimonial_show.php
The shortcode callback shouldn't produce an output but should return the result.
You can find this on documentation website here. Please pay attention to
Note that the function called by the shortcode should never produce an
output of any kind. Shortcode functions should return the text that is
to be used to replace the shortcode. Producing the output directly
will lead to unexpected results. This is similar to the way filter
functions should behave, in that they should not produce expected side
effects from the call since you cannot control when and where they are
called from.
Here, simply you can create the shortcode in the functions.php file and on your Page/Posts you can use the Text Module and add the [shortcode].
I'm using one of the plugins inside Wordpress which needs to be rendered with PHP because I want to use it as shortcode inside WPbakery.
Developer instructions for displaying in custom location is this line of code:
<?php wccf_print_product_field(array('key' => ‘mykey’)); ?>
I tried to add this in shortcode function like:
function newshortcode1() {
wccf_print_product_field(array('key' => ‘mykey’));
}
add_shortcode( 'newshortcode', 'newshortcode1' );
But sadly this doesn't work no matter how I change this code.
I'm using [newshortcode] insite post to display it.
Any suggestions what I'm doing wrong here?
You are using ‘ (apostrophes) instead of ' (Single quotes).
So, update from:
wccf_print_product_field(array('key' => ‘mykey‘));
to:
wccf_print_product_field(array('key' => 'mykey'));
Firstly a shortcode function needs to return the desired output (It essentially replaces the shortcode text in the content). IF wccf_print_product_field returned the desired output, then you could do
return wccf_print_product_field
HOWEVER I strongly suspect that wccf_print_product_field prints or echo's the output directly when called. So it would be doing it when wordpress calls apply_filters to the page content, NOT returning it to the shortcode text.
So if you really must use it in a shortcode (if there is no other function that just returns the product field), then you will have to trap the output so you can return it in the shortcode. Something like:
function newshortcode1() {
ob_start(); /* catch the output, so we can control where it appears in the text */
wccf_print_product_field(array('key' => ‘mykey’));
$output .= ob_get_clean();
return $output;
If you view source on the page with the shortcode as it is now, I'd bet that you see your product field somewhere earlier in the page where it shouldn't be, possibly right at the start.
I am learning to write a WordPress short code.
In my function I created a big table and some CSS. When I put my short code into posts, the position of the table is always on the top of the posts, regardless of where I put the code in the editor.
How can I fix this?
Shortcode content should be return. not echo. user below:
function shortcodetest(){
return "<table><tr><td>test</td></tr></table>";
}
add_shortcode("shortcode","shortcodetest");
Do not use echo :
function shortcodetest(){
echo "<table><tr><td>test</td></tr></table>";
}
add_shortcode("shortcode","shortcodetest");
Anyone familiar with WordPress shortcodes? I could really use a hand! I've inserted the following code into my functions.php file for the theme I'm using...
function create_slideshow_header($atts, $content = null){
return '<div class="item_heading">'.$content.'</div>';
}
add_shortcode('slideshow_heading', 'create_slideshow_header');
function create_slideshow_white_header($atts, $content = null){
return '<span id="dyn">'.$content.'</span>';
}
add_shortcode('slideshow_heading_white', 'create_slideshow_white_header');
function create_slideshow_content($atts, $content = null){
return '<div class="item_content">'.$content.'</div>';
}
add_shortcode('slideshow_content', 'create_slideshow_content');
Now, I was led to believe by several tutorials that this should allow me to insert the following into the text editor in the WP backend...
[slideshow_heading]SLIDESHOW HEADER[/slideshow_heading]
...and the SLIDESHOW HEADER text would be wrapped in the appropriate HTML.... but it's just displaying the above as regular text. I've cleared my cache, etc...
Is there something I'm doing wrong? Thanks in advance!
SOLUTION
I failed to mention that I was using the page.ly MultiEdit plugin--which uses "custom fields" to create extra editable regions. WordPress conveniently doesn't parse shortcodes in custom fields. Normally, you can create a filter for each custom field, but since this is a plugin, you can just edit the multiedit.php file, and change line 63 from
echo $GLOBALS['multiEditDisplay'][$index][0];
to
echo apply_filters('the_content',$GLOBALS['multiEditDisplay'][$index][0]);
With a little work, you can turn Wordpress into a truly amazing CMS!
I actually checked out your code, it works for me. Your usage is right.
Try adding a die() in there to see whether the method is called in your case.