• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

BUG Passing parameters to PHP functions through snippets

Messages
7
Likes
0
Points
1
#1
Alex posted an explanation in the WordPress forum about how to do this. He said:
In the new version of the plugin I made it possible to pass the value through shortcodes. You can get the values of the variables from shortcode attributes. For example, if you set the my_type attribute for the shortcode [wbcr_php_snippet id="2864" my_type="button"], you can get the value of the my_type attribute in the snippet by calling $my_type var.

And here is my question:
Following the explanation above, I created the following snippet:
insertBlurb($imageCode);
where insertBlurb is a function defined in custom.php using the Thesis theme.
When I place the shortcode in a page as follows:
[wbcr_php_snippet id="8931" imageCode="AA"]
the function does not execute properly as expected --- nothing appears on the page.

HOWEVER, if I change the snippet like this:
insertBlurb('AA');
giving the actual value of the variable in the content of the snippet, the function executes properly and places the appropriate text on the page.

What is the problem with the first method? I want to be able to define a single snippet that can accept parameters and pass them on to a PHP function. The second method would require an inordinate number of unmanageable snippets. Any insight you can offer would be much appreciated,

Thank you for your assistance.
Timothy Binkley
 

Kirill

Support team
Messages
82
Likes
4
Points
8
#2
Hi
I guess you have case-sensitive comparisons for $imageCode. The problem is that the attributes of the shortcode are filtered using sanitize_title(), which translates string into lower case and escapes some characters.
You can disable attribute filtering.
To do this, comment out the line
PHP:
$attr = array_map('sanitize_title', $attr);
in file
wp-content/plugins/insert-php/includes/shortcodes.php.

We will fix this in the next update.

Let me know if this doesn't solve the problem.

Best regards
 
Messages
7
Likes
0
Points
1
#3
Hi Kirili
Thank you for your response. Commenting out that line did not seem to correct the problem.
I also tried adding the following line to my function insertBlurb($imageCode):
$imageCode = strtoupper($imageCode);
And that did not make any difference.
Please let me know if you have any other suggestions.
Best Regards
- Tim
 

Kirill

Support team
Messages
82
Likes
4
Points
8
#4
Let's check something.
Add a follow line to the snippet and go to the page:
PHP:
var_dump( get_defined_vars() );
Copy the text that appears here.
I think it contains the desired variable in some form.

Best regards
 
Messages
7
Likes
0
Points
1
#5
Thanks for the suggestion Kirili. The page is called "blurb" and so is the snippet. Here is the dump that appears:

array(8) { ["attr"]=> array(2) { ["id"]=> string(4) "8931" ["imagecode"]=> string(2) "AA" } ["content"]=> string(0) "" ["id"]=> int(8931) ["snippet_meta"]=> array(6) { ["_edit_lock"]=> array(1) { [0]=> string(12) "1544660553:1" } ["_edit_last"]=> array(1) { [0]=> string(1) "1" } ["wbcr_inp_snippet_activate"]=> array(1) { [0]=> string(1) "1" } ["wbcr_inp_snippet_code"]=> array(1) { [0]=> string(63) "insertBlurb($imageCode); var_dump( get_defined_vars() ); " } ["wbcr_inp_snippet_scope"]=> array(1) { [0]=> string(9) "shortcode" } ["wbcr_inp_snippet_description"]=> array(1) { [0]=> string(0) "" } } ["is_activate"]=> bool(true) ["snippet_code"]=> array(1) { [0]=> string(63) "insertBlurb($imageCode); var_dump( get_defined_vars() ); " } ["snippet_scope"]=> string(9) "shortcode" ["imagecode"]=> string(2) "AA" }

The contents of page "blurb" are just:
[wbcr_php_snippet id="8931" imageCode='AA']

And I've added your code to the "blurb" snippet:
insertBlurb( $imageCode);
var_dump( get_defined_vars() );

The function did not write the desired text on the page (as usual) but, as you expected, the variable and its assigned value appear twice.
 

Kirill

Support team
Messages
82
Likes
4
Points
8
#6
I think you should use the names of the parameters in lower case only. Example: use imagecode instead imageCode.

Best regards
 

Kirill

Support team
Messages
82
Likes
4
Points
8
#8
For the parameter value - yes.
For the parameter name - no. Sorry, but this is rule for shortcodes in WordPress.
Attribute names are always converted to lowercase before they are passed into the handler function.
Wordpress API

Best regards