Skip Navigation

[Resolved] Cannot render the shortcode in View

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here is that the user had written up a custom shortcode below.


function get_dealer_url( $atts) {
    global $post;
    $args = array( 'posts_per_page' => 1, 'post_type'=> 'dealer', 'author_name' => $atts('author') );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) {
        setup_postdata($post);
        return the_permalink();
    }
}
add_shortcode('get_dealer_url', 'get_dealer_url');


However their shortcode was not rendering on the frontend.

Solution:

Issues like these actually requires that you debug the shortcodes. Now since this is custom coding we don't normally debug custom codes.

However since this was a simple implementation a little discretion was used.

In our Toolset Custom Code editor, your shortcode can't be activated if an error is detected and this was the case with this user.

Checking the code we can see that the user has 'author_name' => $atts('author')

Where $atts is an associative array. Now the correct way to slice this array with the key is $atts['author'] using square brackets instead of the round ones.

So I would recommend going through your code to ensure that the correct calls are being made

This support ticket is created 5 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 13 replies, has 2 voices.

Last updated by kelvinL-2 5 years, 10 months ago.

Assisted by: Shane.

Author
Posts
#1221989
Screenshot 2019-03-28 at 4.04.05 AM.png
Screenshot 2019-03-28 at 4.02.45 AM.png

Dear Sir/Madam,

I have two shortcodes [check_if_dealer] and [get_dealer_url], I registered both shortcodes in Third-party shortcode arguments, but don't know why only one can be rendered, what syntax I did wrong to another one?

            <!-- Dealer id: [wpv-post-author] -->
[wpv-if  evaluate="'[check_if_dealer]'=1"]
            <!-- [check_if_dealer] , [get_dealer_url author='[wpv-post-author]'] -->
[/wpv-if]            

I added the script in functions.php

function get_author_role()
{
    global $authordata;

    $author_roles = $authordata->roles;
    $author_role = array_shift($author_roles);

    return $author_role;
}

function check_if_dealer() {
    if (get_author_role() == "dealer") {
		$rtn = 1;
	} else {
		$rtn = 0;
	}
	return $rtn; 
}
add_shortcode('check_if_dealer','check_if_dealer');

function get_dealer_url( $atts) {
    global $post;
    $args = array( 'posts_per_page' => 1, 'post_type'=> 'dealer', 'author_name' => $atts('author') );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) {
        setup_postdata($post);
        return the_permalink();
    }
}
add_shortcode('get_dealer_url', 'get_dealer_url');
#1222007

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Kelvin,

Thank you for contacting our support forum.

I see you are using our old method of doing conditionals. Could you update your conditional to use the new method.

In your case it would be


[wpv-conditional if="( '[check_if_dealer]' eq '1' )"] 

[/wpv-conditional]

The documentation on this can be found below.
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/

Please let me know if this helps.
Thanks,
Shane

#1222139
Screenshot 2019-03-28 at 4.04.05 AM.png

Dear Shane,

Yes, thanks for your update. I will update the condition but it can't solve my question.

My question is the shortcode could not be rendered, I have two shortcodes [check_if_dealer] and [get_dealer_url] but only [check_if_dealer] could be rendered. Refer to the screenshot, you can find the [get_dealer_url ....] didn't be rendered. What is the problem?

Best regards,

Kelvin

#1222450

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Kelvin,

If you remove the comment from the shortcode does the code still not render?

Also let me know if the shortcode is activated. Was this placed in our custom code section in Toolset -> Settings -> Custom code?

Please let me know.
Thanks,
Shane

#1223306
1221989-Screenshot_2019_03_28_at_4.04.05_AM.png
Screenshot 2019-03-30 at 2.35.48 PM.png
Screenshot 2019-03-30 at 2.34.54 PM.png
Screenshot 2019-03-30 at 2.30.11 PM.png

Dear Shane,

I don't think it is affected by the comment <!-- -->, if you view the screenshot carefully, you can find the [check_if_dealer] was rendered but not the [get_dealer_url ....]

I don't have the idea to the Custom code from Toolset > Setting, I put the PHP code of [check_if_dealer] in functions.php, it works but [get_dealer_url ....]

I added the snippet get_dealer_url, it created a script "get_dealer_url.php" under wp-content/toolset-customizations/get_dealer_url.php, what is the difference compared to functions.php

When I place my get_dealer_url() from my fuctions.php into the Custom code snippets, I click Save and Test, I got

There was an error when updating snippets.

I don't know what error it is.

I activated the code snippet, but no help to it.

Please note the check_if_dealer is also inside the comment, get_dealer_url doesn't work (see screenshot)

 <!-- Dealer id: [wpv-post-author] -->
[wpv-if  evaluate="'[check_if_dealer]'=1"]
            <!-- [check_if_dealer] , [get_dealer_url author='[wpv-post-author]'] -->
[/wpv-if]     
#1223951

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Kelvin,

There seems to be something that is preventing your snippet from being saved and activated.

Could you try adding the snippet to the functions.php file and let me know if the results are the same ?

If adding it to the functions.php file generates an error on the frontend then this is something we need to resolve in the snippet.

Thanks,
Shane

#1226833

Dear Shane,

If I add it to functions.php, I will get error

Fatal error: Cannot redeclare get_dealer_url() (previously declared in /home/hircar/public_html/hosting/hicar.com.hk/www/wp-content/themes/toolset-starter-child/functions.php:4) in /home/hircar/public_html/hosting/hicar.com.hk/www/wp-content/toolset-customizations/get_dealer_url.php on line 16
#1227281

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Kelvin,

Could you provide me with access to the site to check this out for you ?

I certainly need to dive deeper into this one for you.

Thanks,
Shane

#1227282

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Also please let me know the page where i can test this on the frontend.

#1227286

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Kelvin no need to resend the credentials, i was able to find them in another ticket.

#1227287

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

All I need no is a URL to the page that I can test this.

Thanks,
Shane

#1227333

Dear Shane,

You can test from hidden link, if page is gone, you can click any car from the home page.

From the web inspector, you can search get_dealer_url.

The shortcode is placed at hidden link

[wpv-if  evaluate="'[check_if_dealer]'=1"]
            <!-- [check_if_dealer] , [get_dealer_url author='[wpv-post-author]'] -->
[/wpv-if]  
#1227400

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Kelvin,

I was able to find the issue.

It was actually this line $atts['author'] , what you had was $atts('author')

The issue now is that the url is now displaying at the top of the page.

What is likely happening here is that the setup_postdata() function is using the echo function to display the url.

Thanks,
Shane

#1230534

Dear Shane,

Thanks a lot.