• Resolved WP CMS Ninja

    (@billiardgreg)


    you have included a few default shortcodes for modifying the output of a given string.  I attempted to create my own shortcode for use within this and was not able to get it to work without having modified the do_shortcode functionion within the url-transform.php file. How do you recommend me doing this type of thing. For instance, i need to take a string1_string2 and remove the underscore and ucwords the string then add the underscore back in so the new redirected URL is String1_String2 due to canonical URLs on the landing page.  

    add_shortcode('ucase_word','ucase_word');function ?ucase_word  ( $attrs, $content, $tag ) {
        $tmp_content = str_replace( [ '_' ], ' ', $content );
        $tmp_content = ucwords($tmp_content);
        $tmp_content = str_replace( [ ' ' ], '_', $tmp_content );
        return $tmp_content;
    }

    ?I wrote this but when I try to use this  ucase_word shortcode it just outputs it into the URL.  Any direction you can give would be greatly appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter WP CMS Ninja

    (@billiardgreg)

    I think I found the way by tying in with the redirection_url_transform and redirection_shortcodes filters.

    Plugin Author John Godley

    (@johnny5)

    I’m not sure what you are trying to do, but using a shortcode is maybe the hard way and it might be easier to use one of the filters listed here:

    https://redirection.me/developer/wordpress-hooks/

    Thread Starter WP CMS Ninja

    (@billiardgreg)

    I was able to accomplish the shortcode method by using the following code in my theme’s function file.

    add_filter('redirection_shortcodes', function( $sc_array ) {
            $sc_array[] = 'ucase';
            return $sc_array;
        }
    );
    
    add_filter('redirection_url_transform', function( $nothing, $tag, $attrs, $content) {
            switch ( $tag ) {
                case 'ucase':
                    $tmp_content = str_replace( [ '_', '-' ], ' ', $content );
                    $tmp_content = ucwords($tmp_content);
                    $content = str_replace( [ ' ' ], '_', $tmp_content );
                    return $content;
                break;
            }
        },99,4
    );
    

    Using this I added the ucase shortcode to the list of available shortcodes, then added in my own url_transform switch based off of the tag.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom Shortcode for Target URL’ is closed to new replies.