Skip Navigation

[Resolved] Strlen and conditional not working with string that have link in it

This support ticket is created 5 years, 5 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

Tagged: 

This topic contains 18 replies, has 2 voices.

Last updated by philippeK 5 years, 5 months ago.

Assisted by: Nigel.

Author
Posts
#1396207

Hello
this is a follow up of this support ticket: https://toolset.com/forums/topic/empty-function-on-view-behaves-strangely-on-one-page/
Still the same issue, I did some test and Toolset seems to behaves in strange ways, here is the code:

In functions.php, I wrote a shortcodes to display the taxonomy of a custom post type but with a link to something different then the normal taxonomy link, I did 2 versions, one with the shortcode that return the full link and one with just the names of the taxonomies. I also wrote a shortcode to get the strlen of a string:

add_shortcode( 'wpv_taxonomy_list_for_one_post_nobug', 'wpv_taxonomy_list_for_one_post_nobug_func' );
function wpv_taxonomy_list_for_one_post_nobug_func( $atts ) {
    extract( shortcode_atts( array(
        'post_id' => ''
    ), $atts ) );
     
    $interview_terms = wp_get_object_terms( $post_id,  'interview-theme' );
    $return = "";
     
    if ( ! empty( $interview_terms ) ) {
        if ( ! is_wp_error( $interview_terms ) ) {
                foreach( $interview_terms as $term ) {
                    $return .= $term->name . ' '; 
                }
        }else return "";
    }else return "";
     
    return $return;
}
 
add_shortcode( 'wpv_taxonomy_list_for_one_post', 'wpv_taxonomy_list_for_one_post_func' );
function wpv_taxonomy_list_for_one_post_func( $atts ) {
    extract( shortcode_atts( array(
        'post_id' => ''
    ), $atts ) );
     
    $interview_terms = wp_get_object_terms( $post_id,  'interview-theme' );
    $return = "";
     
    if ( ! empty( $interview_terms ) ) {
        if ( ! is_wp_error( $interview_terms ) ) {
                foreach( $interview_terms as $term ) {
                    $return .= '<a href="' . esc_url( get_bloginfo('url') . '/peace-media-library/?wpv-interview-theme=' . $term->slug ) . '">' . esc_html( $term->name ) . '</a> - '; 
                }
        }else return "";
    }else return "";
     
    $return = substr($return, 0, -3);
    return $return;
}
 
// get all taxonomy for one interview with link to the Main media library search page
add_shortcode( 'wpv_get_length_string', 'wpv_get_length_string_func' );
function wpv_get_length_string_func( $atts ) {
    extract( shortcode_atts( array(
        'string' => ''
    ), $atts ) );
     
    return strlen($string);
}

Then in my view I tested each functions to see how it's behaves:

<h1> BUG: </h1>
      Taxonomy: [wpv_taxonomy_list_for_one_post post_id="[wpv-post-id]"] <br>
      Count bug because of special chars??? : [wpv_get_length_string string="[wpv_taxonomy_list_for_one_post post_id="[wpv-post-id]"]"] <br>
      Taxonomy without link: [wpv_taxonomy_list_for_one_post_nobug post_id="[wpv-post-id]"]<br>
      Count no bug: [wpv_get_length_string string="[wpv_taxonomy_list_for_one_post_nobug post_id="[wpv-post-id]"]"] <br>      
      
      Conditionnal not working either way 1: 
    [wpv-conditional if="('[wpv_taxonomy_list_for_one_post post_id="[wpv-post-id]" ]' ne '' )" ]
<p class="interview-many-taxonomies"><i class="fad fa-align-justify" style=""></i> [wpv_taxonomy_list_for_one_post post_id="[wpv-post-id]" ]</p>
[/wpv-conditional]
       
      Conditionnal not working either way 2:
    [wpv-conditional if="('[wpv_taxonomy_list_for_one_post_nobug post_id="[wpv-post-id]"]" ]' ne '' )" ]coucou
<p class="interview-many-taxonomies"><i class="fad fa-align-justify" style=""></i> [wpv_taxonomy_list_for_one_post post_id="[wpv-post-id]" ]</p>
[/wpv-conditional]

Here is the result hidden link
as you can see, the shortcode that count the number of characters doesn't work with a string that contains link.
And then when I do a conditional on the taxonomy it isn't triggered.

#1396261

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Without looking too closely at your code, I see problems with quotes.

For example:

[wpv_get_length_string string="[wpv_taxonomy_list_for_one_post post_id="[wpv-post-id]"]"]

needs to be re-written to mix double and single quotes, e.g.

[wpv_get_length_string string="[wpv_taxonomy_list_for_one_post post_id='[wpv-post-id]']"]

Also, WordPress doesn't support using shortcodes for the values of shortcode attributes in this way.

Views does, it has a custom shortcode parser to handle this scenario, but you need to add the shortcodes you will use inside shortcode attributes in the settings, at Toolset > Settings > Front-end Content, before they can be used.

Try fixing those two issues, and if you still have problems, let me know.

#1396275

I modified the view code to mix double and single.
I had registered the shortcodes in Toolset->Settings before testing the code.
I doesn't change the way it behaves

#1396301

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

OK, try breaking it up.

What does the shortcode on its own output?

[wpv_taxonomy_list_for_one_post post_id='[wpv-post-id]']

If it outputs a string with a series of anchor tags what happens when you copy and paste that string and feed it directly into

[wpv_get_length_string string='pasted result from the first shortcode']
#1396319
[wpv_get_length_string string='<a href="<em><u>hidden link</u></em>">forgiveness</a>']

it outputs: 117
so it works with direct value

#1396323

I put a debug on the first conditional:
####################
wpv-conditional attributes
####################
Array
(
[if] => ('' ne '' )
[debug] => true
)

####################
Debug information
####################
--------------------
Original expression: ('' ne '' )
--------------------
After replacing 1 general variables and comparing strings: ('' ne '' )
Comparing to

it shows up only when there is no taxonomy

#1396629

to be sure I'm using the double and single quote correctly on long conditional:

[wpv-conditional if="('[wpv_nbr_child_post parent_post_id='[wpv-post-id item='@person-1min-interview.parent']' what_content='interview']' gt '1' )" debug="true"]

is this the correct way to include a shortcode in an other shortcode?
I will check my all code to see if there is no error regarding the use of that.

#1397319

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

That looks correct, yes, in your last update, the key part being that the entire if attribute should be contained inside a set of double quotes, with no other double quotes being used inside.

I don't think you answered my first question, what does your shortcode [wpv_taxonomy_list_for_one_post post_id='[wpv-post-id]'] output on its own?

You showed including the link tag directly in the second example worked, but I don't see what the result of the first question was.

#1397335

sorry here it is:

[wpv_taxonomy_list_for_one_post post_id='[wpv-post-id]'] return that: 
<a href="<em><u>hidden link</u></em>">forgiveness</a>
#1397685

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

I was going to suggest you drop the post_id attribute to simplify things.

You insert the shortcodes in a View which iterates over some posts. The global $post object is updated in each case to the current post, so instead of passing the post id, you can simplify things by using the global $post object in wp_get_object_terms instead of post_id.

That would remove one potential failure point.

It certainly worked in my local testing, where I'm using a "Colour" taxonomy and created the following simple shortcode:

add_shortcode('term-link', function ($atts = [], $content = null) {

    global $post;
    $return = "";
    $colours = get_the_terms( $post, 'colour' );

    foreach ( $colours as $colour ){
        $return .= $colour->name . ' ';
    }

    return $return;
});

and included it in a conditional shortcode like so:

[wpv-conditional if="( '[term-link]' ne '' )"]<p>Colours: [term-link]</p>[/wpv-conditional]

But to double-check, I edited my shortcode so that it did pass the post_id as an attribute using wpv-post-id shortcode for the value, and it also worked.

Which leads me to think the shortcodes may not have been registered correctly at Toolset > Settings > Front-end content.

Could you show me a screenshot of the relevant part?

#1397725
Untitled-1.png

I'll check your code later today, in the meanwhile, here is a screenshot:

#1398041

your solution with global $post works, but the issue is still there. When I return a link with my short code it doesn't work and when I do something like your code it works, I tried to create a new view with a simple display, you can see the result.
hidden link

<h2>[wpv-post-title]</h2>
taxonomy with link: [wpv_taxonomy_list_for_one_post post_id='[wpv-post-id]' ]<br>
length with link: [wpv_get_length_string string="[wpv_taxonomy_list_for_one_post post_id='[wpv-post-id]' ]"]<br>
length without link: [wpv_get_length_string string="[wpv_taxonomy_list_for_one_post_nobug post_id='[wpv-post-id]' ]"]<br>

Conditionnal without link works ok: 
	[wpv-conditional if="('[wpv_taxonomy_list_for_one_post_nobug post_id='[wpv-post-id]']' ne '' )" debug="true"]
<p class="interview-many-taxonomies"><i class="fad fa-align-justify" style=""></i> [wpv_taxonomy_list_for_one_post post_id='[wpv-post-id]' ]</p>
[/wpv-conditional]

Conditionnal with link doesn't work: 
	[wpv-conditional if="('[wpv_taxonomy_list_for_one_post post_id='[wpv-post-id]']' ne '' )" debug="true"]
<p class="interview-many-taxonomies"><i class="fad fa-align-justify" style=""></i> [wpv_taxonomy_list_for_one_post post_id='[wpv-post-id]' ]</p>
[/wpv-conditional]

and my shortcodes in functions.php

add_shortcode( 'wpv_taxonomy_list_for_one_post_nobug', 'wpv_taxonomy_list_for_one_post_nobug_func' );

function wpv_taxonomy_list_for_one_post_nobug_func( $atts ) {
	global $post;
	$interview_terms = get_the_terms( $post,  'interview-theme' );
 	$return = "";
	
	if ( ! empty( $interview_terms ) ) {
		if ( ! is_wp_error( $interview_terms ) ) {
				foreach( $interview_terms as $term ) {
					$return .= $term->name . ' '; 
				}
		}else return "";
	}else return "";
	
	return $return;
}


add_shortcode( 'wpv_taxonomy_list_for_one_post', 'wpv_taxonomy_list_for_one_post_func' );
function wpv_taxonomy_list_for_one_post_func( $atts ) {
	global $post;
	$interview_terms = get_the_terms( $post,  'interview-theme' );
 	$return = "";
	
	if ( ! empty( $interview_terms ) ) {
		if ( ! is_wp_error( $interview_terms ) ) {
				foreach( $interview_terms as $term ) {
					$return .= '<a href="' . esc_url( get_bloginfo('url') . '/peace-media-library/?wpv-interview-theme=' . $term->slug ) . '">' . esc_html( $term->name ) . '</a> - '; 
				}
		}else return "";
	}else return "";
	
	//$return = substr($return, 0, -3);
	return $return;	
}

// get all taxonomy for one interview with link to the Main media library search page
add_shortcode( 'wpv_get_length_string', 'wpv_get_length_string_func' );
function wpv_get_length_string_func( $atts ) {
  	extract( shortcode_atts( array(
        'string' => ''
    ), $atts ) );
	
	return strlen($string);
}

clearly there is a problem with there is a string with link in it that is return by the shortcode. I either have a bug in the way I do my link in wpv_taxonomy_list_for_one_post_func or Toolset doesn't process it in a correct way.

#1398079

I tried to simply return that for the short code wpv_taxonomy_list_for_one_post_func:

	return '<a href="#grid-options">Grid options</a>';

it doesn't work and as soon as I remove the double quote from href link it works. So there is cleary a problem with a string that has double quote inside it.

#1398353

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

OK, yes, I checked this as well, and if your shortcode returns a string with quotes it breaks the condition just as if you had add a string directly that contains. That actually makes sense, but we don't appear to document it, so I'm creating a ticket for the documentation team to make that explicit in the relevant documentation.

In your case you'll need an alternative strategy for your shortcodes that are used in the conditional shortcodes.

Instead of returning the full string of the links which include quotes, you'll want a separate version of the same shortcode which can be used in the condition which returns, say, the length of the string, or 1 and 0 for true or false, something like that.

#1398393

ok, I see...
But there is one problem then:
I have a custom field video-quote, sometimes the content contains a double quote, so this code is not working when I have a double quote on my content:

[wpv-conditional if="('[types field='video-quote' output='raw'][/types]' ne '' )" debug="true"]
<strong>OK: [types field='video-quote'][/types]</strong><br>
[/wpv-conditional]

So, how am I suppose to handle those kind of content ? Do I have to create a shortcode that will check for a custom field length so that I bypass the shortcode ?