Skip Navigation

[Resolved] PHP Script Not Working In Content Template

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 8 replies, has 2 voices.

Last updated by Minesh 1 year, 2 months ago.

Assisted by: Minesh.

Author
Posts
#2642733

Tell us what you are trying to do?
The following PHP script is added to a content template. The only output is "test2" which is from the echo statement that is outside of the function. The function pmpro_hasMembershipLevel is used successfully in other script. Input on why this is not working?

function conditional_role() {
$return = 0;
$current_user = wp_get_current_user();

if (pmpro_hasMembershipLevel('Elite', $current_user))
{
$return = 1;
}

echo $return;
echo $current_user;
echo "test";
return $return;
}
echo "test2";

add_shortcode('conditional_roles2', 'conditional_role' );

The shortcode is registered here: Toolset Settings - Front-end Content - Third-party shortcode arguments

Third-party shortcode arguments
Shortcodes registered automatically

[cred-post-expiration] [wpml-string]

Shortcodes registered manually

List of custom and third-party shortcodes you want to be able to use as Views shortcode arguments.

[conditional_roles]
[conditional_roles2]

Is there a similar example that we can see?
hidden link
Shortcode at bottom of this post.

#2642801

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

You should not add PHP script to content template.

The correct way is you should add your PHP script to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

Then you can use the shortcode [conditional_roles2] to call that script from your content template.

More info:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/

#2642863

Hello Minesh,

Thank you for the reply.

To clarify, I added the PHP script to a snippets plugin and used the shortcode to call the script. The shortcode is in the content template - [conditional_roles2]. I believe the script runs, because it echos "test2", which is outside the function. However, none of the echo statements inside the function execute, and I believe that they should.

I plan to use this in a conditional statement, so should echo 0 or 1 for $return.

#2642975

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please share admin access details and link where you added the shortcode and where you are displaying it on frontend.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2643147

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

When I try to access the wp-admin, it shows the following message "Sorry, you are not allowed to access this page.",

Can you please make sure you have granted admin user rights to the user you shared.

#2643151

Please try again. I had the Role set incorrectly for the user and have set it to Administrator now.

#2643155

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I checked first the URLs you shared:
The PHP script is added in the Code Snippets plugin here:
hidden link
Notice it is set to run everywhere.

In Woody Snippets, the PHP script is here:
hidden link
This is only set to run where the shortcode [wbcr_php_snippet id="26203"] is used.

When I checked both of the links above I see you are using the same function name and same shrotcode with both the snippet.

The function and shortcode name should be unique. Can you please correct that first.

Also, with the Woody Snippets, the PHP script is here:
hidden link
==>
I do not have rights to update the snippet I can not able to edit and update the snippet.

Also, I'm not sure why you are using other snippet plugins where Toolset offers the "Custom Code" section to add custom snippet.

What if you try to add sample shortcode to "Custom code" snippet:

function func_print_hello_world($atts) {
    return 'Hello World';
}
add_shortcode('printhello', 'func_print_hello_world');

And to content template add the following shortcode:

[printhello]

Is it working, if yes, then what if you try to add delete the above shortcode from "Custom Code" section and then try to add it to the place where you want and do you see it working?

#2643221

Minesh,

I added the PHP script for [printhello] to the Toolset Custom Code section. When I add the shortcode to the content template, the output is:
Hello World

It looks like it works there. I did not remember the Toolset Custom Code area. I will add the PHP script for the conditional_roles function to the Toolset Custom Code to see if it works.

#2643223

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Please make sure to remove the code snippet elsewhere as you can not add same function with same shortcode multiple times as communicated before it should be unique.

#2643255

Minesh,

This works if I add the PHP script in the Toolset Custom Code section, and it did not work in either of the two snippet plugins.

Thank you for the prompt reply and supporting in resolving this issue!