Skip Navigation

[Resolved] Shortcode to test for string in a variable stopped working with latest updates

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.

Tagged: 

This topic contains 13 replies, has 2 voices.

Last updated by martinE-4 2 months, 1 week ago.

Assigned support staff: Luo Yang.

Author
Posts
#1829247

Nigel helped me with this great function:

add_shortcode('is-local', function ($atts = [], $content = null) {
 
    if ( !is_null($content) ){
 
        $site_url = get_bloginfo( 'url' );
        $content = do_shortcode( $content );
 
        $is_local = strpos( $content, $site_url );
    }
 
    return ( $is_local === 0 ) ? 1 : 0;
});

Recently I updated my staging site and it ceased to work. WordPress 5.5.2 and the latest Views and Types plugins.

However, I have to point out that it DOES work on my localhost which seems to have only two differences that I can spot from the online site. (I run php 7.3.5 vs 7.3.23 online; and the online WordPress site health points to an 'optional' php module 'fileinfo' missing).

#1830009

Update: I upgraded the staging site to PHP 7.4.11 and it still does not work. I also added 'fileinfo' php module but that did not change anything.

The shortcode is still not working on the staging site? I'll check themes and plugins again but I am thinking it is not that because all is working fine on my localhost!

#1830141

I added some echo statements to the function and changed the code to make it simpler although less elegant:

add_shortcode('is_local', 'is_local_func');

function is_local_func ($atts = [], $content = null) {
 
     if ( !is_null($content) ){
        $site_url = get_bloginfo( 'url' );
        $content = do_shortcode( $content );
    echo "Content: " . $content . "<br>";
        // Test if string contains the word 
if(strpos( $content, $site_url ) !== false){
    $is_local = 1;
    echo "Url string Found! " . "is_local: " . $is_local . "<br>";
    
} else{
    $is_local = 0;
    echo "Url string Not Found! " . "is_local: " . $is_local . "<br>";    
}
    } 
    return $is_local;
};

This executed fine on my localhost with output to the screen at top of page:
Content:
Url string Not Found! is_local: 0
Content:
Url string Not Found! is_local: 0
Content: hidden link..png
Url string Found! is_local: 1
Content: hidden link..png
Url string Found! is_local: 1
Content: hidden link
Url string Found! is_local: 1
Content: hidden link
Url string Found! is_local: 1
and so on for the post loop...

This also executed fine on my staging site with output to the screen at top of page:
Content:
Url string Not Found! is_local: 0
Content:
Url string Not Found! is_local: 0
Content:
Url string Not Found! is_local: 0
Content:
Url string Not Found! is_local: 0
Content:
Url string Not Found! is_local: 0
Content:
Url string Not Found! is_local: 0
Content:
and so on for the post loop...

It appears that on the site host, $content is always empty! Does this mean the statement:

$content = do_shortcode( $content );

is wrong??? (although it all seems fine on localhost)

I am slightly puzzled on both hosts that the first iteration appears to be empty. For some reason, although there are 10 posts paginated in the loop, my function is fired 11 times from the view loop...

#1830357

Extra Info: I used a snippet titled "Check for Local URL" to define the shortcode function in the Snippets plugin.

#1830733

I am testing here:
hidden link

[wpv-conditional if="( '[is_local][types field='common_featured_media' output='raw'][/types][/is_local]' eq '0' )"][types field='common_featured_media' ][/types][/wpv-conditional]

[wpv-conditional if="( '[is_local][types field='common_featured_media' output='raw'  ][/types][/is_local]' eq '1' )"]<div><a href="[wpv-post-url]"><img src="[types field='common_featured_media' output='raw' ][/types]"></a></div>[/wpv-conditional]

[wpv-conditional if="( '[types field='common_featured_media' output='raw' ][/types]' eq '' )" ]

<a href="[wpv-post-url]"><img src="[wpv-post-featured-image size='full' output='url' ]"></a>

[/wpv-conditional]

So remember, this all works on my localhost. But on the internet server it doesn't, with the same plugins and toolset versions etc.
The 3rd conditional works. But the first two do not.

I tried outputting just the [types field='common_featured_media' output='raw'][/types] and that looks fine. It must mean the shortcode is getting the correct enclosed content in $content but it is not able to use it in the function or echo it to me. Somehow it is getting lost.

#1830735

I checked my server PHP error log and found this in hundreds of records:

[29-Oct-2020 05:07:41 UTC] WordPress database error Duplicate entry '    Montréal  Canada' for key 'PRIMARY' for query INSERT INTO wp_toolset_maps_address_cache ( `address_passed`, `address`, `point` ) VALUES ( '    Montréal  Canada', 'Montreal, QC, Canada', ST_PointFromText('POINT(-73.567256 45.501689)') ) made by require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), include('/themes/generatepress/single.php'), get_template_part, locate_template, load_template, require('/themes/generatepress/content-single.php'), the_content, apply_filters('the_content'), WP_Hook->apply_filters, do_shortcode, preg_replace_callback, do_shortcode_tag, WPV_Shortcode_Base_View->render, WPV_Shortcode_Post_Body->get_value, apply_filters('the_content'), WP_Hook->apply_filters, Toolset_Addon_Maps_Views->run_shortcodes, do_shortcode, preg_replace_callback, do_shortcode_tag, Toolset_Addon_Maps_Views->marker_shortcode, Toolset_Addon_Maps_Common::get_coordinates, Toolset_Addon_Maps_Common::get_coordinates_new, Toolset_Addon_Maps_Common::update_cached_coordinates
[29-Oct-2020 05:12:19 UTC] WordPress database error Duplicate entry '    Montréal  Canada' for key 'PRIMARY' for query INSERT INTO wp_toolset_maps_address_cache ( `address_passed`, `address`, `point` ) VALUES ( '    Montréal  Canada', 'Montreal, QC, Canada', ST_PointFromText('POINT(-73.567256 45.501689)') ) made by require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), include('/themes/generatepress/single.php'), get_template_part, locate_template, load_template, require('/themes/generatepress/content-single.php'), the_content, apply_filters('the_content'), WP_Hook->apply_filters, do_shortcode, preg_replace_callback, do_shortcode_tag, WPV_Shortcode_Base_View->render, WPV_Shortcode_Post_Body->get_value, apply_filters('the_content'), WP_Hook->apply_filters, Toolset_Addon_Maps_Views->run_shortcodes, do_shortcode, preg_replace_callback, do_shortcode_tag, Toolset_Addon_Maps_Views->marker_shortcode, Toolset_Addon_Maps_Common::get_coordinates, Toolset_Addon_Maps_Common::get_coordinates_new, Toolset_Addon_Maps_Common::update_cached_coordinates
[29-Oct-2020 05:14:54 UTC] WordPress database error Duplicate entry '    Montréal  Canada' for key 'PRIMARY' for query INSERT INTO wp_toolset_maps_address_cache ( `address_passed`, `address`, `point` ) VALUES ( '    Montréal  Canada', 'Montreal, QC, Canada', ST_PointFromText('POINT(-73.567256 45.501689)') ) made by require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), include('/themes/generatepress/single.php'), get_template_part, locate_template, load_template, require('/themes/generatepress/content-single.php'), the_content, apply_filters('the_content'), WP_Hook->apply_filters, do_shortcode, preg_replace_callback, do_shortcode_tag, WPV_Shortcode_Base_View->render, WPV_Shortcode_Post_Body->get_value, apply_filters('the_content'), WP_Hook->apply_filters, Toolset_Addon_Maps_Views->run_shortcodes, do_shortcode, preg_replace_callback, do_shortcode_tag, Toolset_Addon_Maps_Views->marker_shortcode, Toolset_Addon_Maps_Common::get_coordinates, Toolset_Addon_Maps_Common::get_coordinates_new, Toolset_Addon_Maps_Common::update_cached_coordinates
[29-Oct-2020 05:21:49 UTC] WordPress database error Duplicate entry '    Montréal  Canada' for key 'PRIMARY' for query INSERT INTO wp_toolset_maps_address_cache ( `address_passed`, `address`, `point` ) VALUES ( '    Montréal  Canada', 'Montreal, QC, Canada', ST_PointFromText('POINT(-73.567256 45.501689)') ) made by require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), include('/themes/generatepress/single.php'), get_template_part, locate_template, load_template, require('/themes/generatepress/content-single.php'), the_content, apply_filters('the_content'), WP_Hook->apply_filters, do_shortcode, preg_replace_callback, do_shortcode_tag, WPV_Shortcode_Base_View->render, WPV_Shortcode_Post_Body->get_value, apply_filters('the_content'), WP_Hook->apply_filters, Toolset_Addon_Maps_Views->run_shortcodes, do_shortcode, preg_replace_callback, do_shortcode_tag, Toolset_Addon_Maps_Views->marker_shortcode, Toolset_Addon_Maps_Common::get_coordinates, Toolset_Addon_Maps_Common::get_coordinates_new, Toolset_Addon_Maps_Common::update_cached_coordinates
[29-Oct-2020 22:14:21 UTC] WordPress database error Duplicate entry '    Montréal  Canada' for key 'PRIMARY' for query INSERT INTO wp_toolset_maps_address_cache ( `address_passed`, `address`, `point` ) VALUES ( '    Montréal  Canada', 'Montreal, QC, Canada', ST_PointFromText('POINT(-73.567256 45.501689)') ) made by require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), include('/themes/generatepress/single.php'), get_template_part, locate_template, load_template, require('/themes/generatepress/content-single.php'), the_content, apply_filters('the_content'), WP_Hook->apply_filters, do_shortcode, preg_replace_callback, do_shortcode_tag, WPV_Shortcode_Base_View->render, WPV_Shortcode_Post_Body->get_value, apply_filters('the_content'), WP_Hook->apply_filters, Toolset_Addon_Maps_Views->run_shortcodes, do_shortcode, preg_replace_callback, do_shortcode_tag, Toolset_Addon_Maps_Views->marker_shortcode, Toolset_Addon_Maps_Common::get_coordinates, Toolset_Addon_Maps_Common::get_coordinates_new, Toolset_Addon_Maps_Common::update_cached_coordinates
[30-Oct-2020 03:36:52 UTC] WordPress database error Duplicate entry '    Montréal  Canada' for key 'PRIMARY' for query INSERT INTO wp_toolset_maps_address_cache ( `address_passed`, `address`, `point` ) VALUES ( '    Montréal  Canada', 'Montreal, QC, Canada', ST_PointFromText('POINT(-73.567256 45.501689)') ) made by require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), include('/themes/generatepress/single.php'), get_template_part, locate_template, load_template, require('/themes/generatepress/content-single.php'), the_content, apply_filters('the_content'), WP_Hook->apply_filters, do_shortcode, preg_replace_callback, do_shortcode_tag, WPV_Shortcode_Base_View->render, WPV_Shortcode_Post_Body->get_value, apply_filters('the_content'), WP_Hook->apply_filters, Toolset_Addon_Maps_Views->run_shortcodes, do_shortcode, preg_replace_callback, do_shortcode_tag, Toolset_Addon_Maps_Views->marker_shortcode, Toolset_Addon_Maps_Common::get_coordinates, Toolset_Addon_Maps_Common::get_coordinates_new, Toolset_Addon_Maps_Common::update_cached_coordinates
[30-Oct-2020 20:23:27 UTC] WordPress database error Duplicate entry '    Montréal  Canada' for key 'PRIMARY' for query INSERT INTO wp_toolset_maps_address_cache ( `address_passed`, `address`, `point` ) VALUES ( '    Montréal  Canada', 'Montreal, QC, Canada', ST_PointFromText('POINT(-73.567256 45.501689)') ) made by require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), include('/themes/generatepress/single.php'), get_template_part, locate_template, load_template, require('/themes/generatepress/content-single.php'), the_content, apply_filters('the_content'), WP_Hook->apply_filters, do_shortcode, preg_replace_callback, do_shortcode_tag, WPV_Shortcode_Base_View->render, WPV_Shortcode_Post_Body->get_value, apply_filters('the_content'), WP_Hook->apply_filters, Toolset_Addon_Maps_Views->run_shortcodes, do_shortcode, preg_replace_callback, do_shortcode_tag, Toolset_Addon_Maps_Views->marker_shortcode, Toolset_Addon_Maps_Common::get_coordinates, Toolset_Addon_Maps_Common::get_coordinates_new, Toolset_Addon_Maps_Common::update_cached_coordinates
[31-Oct-2020 05:13:18 UTC] WordPress database error Duplicate entry '    Montréal  Canada' for key 'PRIMARY' for query INSERT INTO wp_toolset_maps_address_cache ( `address_passed`, `address`, `point` ) VALUES ( '    Montréal  Canada', 'Montreal, QC, Canada', ST_PointFromText('POINT(-73.567256 45.501689)') ) made by require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), include('/themes/generatepress/single.php'), get_template_part, locate_template, load_template, require('/themes/generatepress/content-single.php'), the_content, apply_filters('the_content'), WP_Hook->apply_filters, do_shortcode, preg_replace_callback, do_shortcode_tag, WPV_Shortcode_Base_View->render, WPV_Shortcode_Post_Body->get_value, apply_filters('the_content'), WP_Hook->apply_filters, Toolset_Addon_Maps_Views->run_shortcodes, do_shortcode, preg_replace_callback, do_shortcode_tag, Toolset_Addon_Maps_Views->marker_shortcode, Toolset_Addon_Maps_Common::get_coordinates, Toolset_Addon_Maps_Common::get_coordinates_new, Toolset_Addon_Maps_Common::update_cached_coordinates

What could this problem be? Is it a separate issue?
I tried de-activating Toolset Maps but it made no difference to the above function that is not working.

#1831231

So I took another approach on the function and wrote one without using do_shortcode. It only works for post meta though, so I am also trying to write one for my term meta.

Here is the post meta one rewritten without do_shortcode. (I notice that there is still an unresolved open ticket as of Views 2.4.1 for using do_shortcode? ( https://toolset.com/errata/unexpected-input-when-shortcode-with-id-attribute-is-rendered-using-php/)

This function works (for post meta only):

add_shortcode('local_file', 'local_file_func');
function local_file_func($atts, $content){
    $atts = shortcode_atts( array(
     'field' => '',
     'post_id' => get_the_ID(),
    ), $atts );
    $field = get_post_meta($atts['post_id'], $atts['field'], true);
    $site_url = get_bloginfo( 'url' );
if(strpos( $field, $site_url ) !== false){
    $is_local = 1;    
} else{
    $is_local = 0;    
}
    $current_post_id = get_the_ID();
 return $is_local;
};
#1831235

Here is my function to handle the term meta situation since the types_render_meta function does not seem to be working:

add_shortcode('local_file_term_meta', 'local_file_term_meta_func');
function local_file_term_meta_func($term_id, $term_taxonomy){
// Retrieve the url from the custom term meta field
	$url_image = get_term_meta( get_queried_object_id() , 'wpcf-common_featured_media', true);
// Retrieve the url of the site
    $site_url = get_bloginfo( 'url' );
// test for the presence of the site url in the custom term meta field -->1=found and is local, 0 = not found
      if(strpos( $url_image, $site_url ) !== false){
      $is_local = 1;    
      } else{
      $is_local = 0;   
      }
 return $is_local;
};

This seems to work fine, and allows me to use the shortcode like [local_file_term_meta field ='wpcf-common_featured_media' ] which returns 1 if it is a 'local' file within my site, or 0 for an external file. If the url of my image is a local file or a YouTube hosted elsewhere, the Views conditional in my archive content template allows me to use the appropriate code to embed the video rather than show an image.

If you have any suggestions for my code, I would appreciate it. Many thanks.

#1831715

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Hello,

Please elaborate the question with more details, where can I see the problem you mentioned above?
What result do you expect to see?

#1832397

This first shortcode function before upgrade worked fine -- used with [is-local][types field='my_custom_field' output='raw' ][/types][/is-local].
It returns 1 if the field contents contains my site url, or 0 if not. In other words, the field contains a url and I need to know if it is locally hosted on my site or if it is an external link.
:

add_shortcode('is-local', function ($atts = [], $content = null) {
  
    if ( !is_null($content) ){
  
        $site_url = get_bloginfo( 'url' );
        $content = do_shortcode( $content );
  
        $is_local = strpos( $content, $site_url );
    }
  
    return ( $is_local === 0 ) ? 1 : 0;
});

This second function I wrote to replace the first function above with a shortcode that did NOT use do_shortcode to make it work -- used with [local_file field='wpcf-my-custom-field']:

add_shortcode('local_file', 'local_file_func');
function local_file_func($atts, $content){
    $atts = shortcode_atts( array(
     'field' => '',
     'post_id' => get_the_ID(),
    ), $atts );
    $field = get_post_meta($atts['post_id'], $atts['field'], true);
    $site_url = get_bloginfo( 'url' );
if(strpos( $field, $site_url ) !== false){
    $is_local = 1;    
} else{
    $is_local = 0;    
}
    $current_post_id = get_the_ID();
 return $is_local;
};

These functions are defined in my Snippets plugin and used in several content templates and pages (eg hidden link). After upgrade, the first function stopped working and always returned 0. When I put 'echo' statements in the first function to debug, it was showing only blank value for $content. This new behavior only occurred after upgrade of wordpress and toolset plugins. It still worked perfectly on my localhost however, even after upgrade!

Since the problem seemed to be an obscure host server issue, I gave up and reprogrammed the function. However, I do think people should be careful using do_shortcode, right? Maybe you should inform Nigel who originally suggested the first function?

Many thanks for thinking about all this. I always seem to be giving you hard and difficult problems! I'm sorry, Luo!

#1834019

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Thanks for the details, I have tried these in your website:
1) Edit the problem page:
hidden link

Modify the shortcodes from:

[wpv-conditional if="( '[local_file field="wpcf-common_featured_media"]' eq '0' )""]
...

To:

[wpv-conditional if="( '[local_file field="wpcf-common_featured_media"]' eq '0' )"]
...

Remove an extra double quotes.

Add two text for test purpose:
local_file is 0
local_file is 1

Test it in front-end:
hidden link

It works fine, I can see different item outputs different results:
local_file is 0
OR
local_file is 1

Can you confirm it?

#1834883

Sorry, you didn't realize that Nigel's original function (see top reply here ) is the one that no longer works with his 'do_shortcode'.

I have re-entered that function in the Snippet area for 'Determine If Local File in Post meta' and then edited the views code that you changed above to make it a shortcode with enclosed content:

[wpv-conditional if="( '[local_file][types field='common_featured_media' output='raw'  ][/types][/local_file]' eq '0' )"]local_file is 0 [types field='common_featured_media' ][/types][/wpv-conditional]

This shortcode (currently active) is the one that no longer worked due to a Toolset change I believe. However, when I rewrote it to be a self closing shortcode NOT requiring enclosed content, I made it work. (as you observed above in your reply to me.). Sorry for the confusion.

I want you to tell me why Nigel's function with the 'do_shortcode' no longer works, please.

#1835147

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Thanks for sharing the solution, that will help other users.

And according to our support policy we don't provide custom support, and the custom codes of Nigel is only an example for your reference.

#1835929

I am glad to share my alternate solution above, this is how I have learned and benefited greatly from other users.