0

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?

2
  • 1
    Is wccf_print_product_field(array('key' => 'mykey')); if you call outside the shortcode? try to change to ' Commented Feb 4, 2019 at 1:27
  • wow, thanks! This really work. Amazing Commented Feb 4, 2019 at 14:28

2 Answers 2

1

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'));

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

Actually I want to display custom field on custom location which is not supported by plugin default. Developer of the plugin direct me to this article support.rightpress.net/hc/en-us/articles/… and I think using as shortcode will be easiest way? Look's like it's not :)
That page describes how to add to a custom page template. The functions echo or print the value. Readup on templates - maybe that will work for you. Or read the plugins code for those functions, see where / how they fetch the values before they output it, and use that code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.