• Resolved Roman

    (@vladroman)


    Hi, Ajay!

    Contextual Related Posts 3.0.7

    I use shortcodes in posts titles (<h1>). For adding support to this feature for your great plugin I use:
    add_filter ('crp_title', function ($title) { return do_shortcode ($title); });

    This works, but not good. If title is long enough, plugin crop it and make shortcode unworkable. As I can see in your code, you add filter after title cropping:

    function crp_title( $args, $result ) {
    
    	$title = crp_trim_char( $result->post_title, $args['title_length'] );  // Get the post title and crop it if needed.
    ...
    	return apply_filters( 'crp_title', $title, $result, $args );
    
    }

    Can I do something to execute shortcodes correctly without editing code of your plugin?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter Roman

    (@vladroman)

    add_filter ('crp_trim_char', function ($string) { return do_shortcode ($string); });
    This has same effect: shortcodes is cropped before executing.

    Thread Starter Roman

    (@vladroman)

    Here is my solution.
    1. Disable title cropping. Didn’t find option for that, so just set 1000 to allowed length.
    2. Execute shortcode and than crop to needed length by filter:
    add_filter ('crp_title', function ($title) {return crp_trim_char (do_shortcode ($title), 55);});

    As I found out later, support to thumb alt/title should be added too. Done by code:

    add_filter ('crp_thumb_title', function ($post_title) {return do_shortcode ($post_title);});
    add_filter ('crp_thumb_alt', function ($post_title) {return do_shortcode ($post_title);});

    Not sure, that my solution is optimal. I didn’t spend a lot of time for reading plugin code. ?? Best solution should be by changing title at once and without editing plugin options. Will be appreciate for any suggestions.

    Thread Starter Roman

    (@vladroman)

    BTW, function crp_trim_char has code:

    if ( 0 === $count ) {
    	return '';
    }

    If I understood right, what you want to do by this code (return empty string, when length setup as 0), code should be:

    if ( 0 === (int) $count ) {
    	return '';
    }

    Or there should be “==”. First variant better.

    Now when title length in plugin settings set to 0, function return full title, because $count is string. This is good for my solution in previous message, but I desided, this is bug, cause “if” has no sense with this logic. ??

    Plugin Author Ajay

    (@ajay)

    Thanks for this – the $count was supposed to be an int always when passed, not a string. But, agree the additional check will help.

    One option for disabling the cropping is setting Limit post title length to a huge number in Outputs tab
    https://ps.w.org/contextual-related-posts/assets/screenshot-3.png?rev=2452385

    Thread Starter Roman

    (@vladroman)

    Thanks for this – the $count was supposed to be an int always when passed, not a string. But, agree the additional check will help.

    When you get this value from DB ($args[‘title_length’]), you have string, not integer. Just put 0 to this option in plugin settings, and you will see, that “if” doesn’t work.

    One option for disabling the cropping is setting Limit post title length to a huge number in Outputs tab

    Yeah, I’ve done this: set 1000. But -1 as turn off is more logical. Not really important, of course. ??

    Thread Starter Roman

    (@vladroman)

    Looks like I have one more problem. I use filters for correcting output, as I wrote before (is this optimal?). BTW, exellent, that you have filters for all important functions. But I do nothing with searching relative post algoritm. So, if I have in DB in post title long shortcode with parametres (like [my_shortcode_name param1=”value1″ param2=”value2″], this has significant effect to this algoritm.

    Can you suggest, what function should I filter with do_shortcode, in order to execute shortcodes after CRP gets posts data and BEFORE CRP uses them for searching relative posts?

    Plugin Author Ajay

    (@ajay)

    The filters you have in there are definitely the right ones to use from what I can see.

    However, do note that the plugin uses mySQL’s full text indexes for the post_title and post_content cells which are processed as is. What that means is that the shortcodes are not preprocessed into the index.

    I’ve been debating about a version of the plugin to generate its own indices in its own tables that would be “processed” but have yet to do so as it is quite complicated.

    Thanks so much for this!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Shortcodes in title’ is closed to new replies.