Support » Plugin: Multisite Language Switcher » Adding filter to customize ahrelang tags

  • Resolved jacksoggetto

    (@jacksoggetto)


    I did an online search, and I’m not the only one to have this need.

    for example someone here asked about x-default:
    https://wordpress.org/support/topic/is-it-possible-to-set-x-default-when-using-multisite-language-switcher/

    adding a filter would help, I added this line:

    $arr = apply_filters( 'mlsl_output_hreflang', $arr );

    on line 124 of the file multisite-language-switcher/includes/MslsOutput.php

    This will allow everyone to manipulate the output it can actually be done better, by returning $arr instead of putting the if in the return, and change the code a bit, so that the return is 100% customizable.

    I’m more than happy to give you soem help with this if you wish.

    Then with this code we can manipulate however we wish:

    
    

    function mlsl_output_edit_hreflang( $arr ) {

    if ( count($arr) == 1)
    return;

    if ( get_msls_permalink( ‘en_US’ ) != ” )
    array_unshift($arr, ‘<link rel=”alternate” hreflang=”x-default” href=”‘ . get_msls_permalink( ‘en_US’ ) . ‘” />’);

    return $arr;

    }
    add_filter( ‘mlsl_output_hreflang’, ‘mlsl_output_edit_hreflang’ );`
    `

    With this code I add an x-default when needed and remove hreflang tags where there are no translations

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Dennis Ploetner

    (@realloc)

    I would love to see this small PR here. Any chance that you make your pull request here?

    https://github.com/lloc/Multisite-Language-Switcher/

    Thread Starter jacksoggetto

    (@jacksoggetto)

    I’ve never done a pull request, hope I did it right, if not, this is what I did:

    Added this 2 lines before the return of get_alternate_links function:

    		$arr = ( 1 === count( $arr ) ) ? $default : $arr;
    		
    		$arr = apply_filters( 'mlsl_output_hreflang', $arr );
    
    		return 1 === count( $arr ) ? $default : implode( PHP_EOL, $arr );
    

    I’m sure it can be done better, this is just a hint to be able to filter the head output

    Plugin Author Dennis Ploetner

    (@realloc)

    So, congrats on your first pull request. 😉

    I’ll answer you over there. There are still some odds to face.

    Thread Starter jacksoggetto

    (@jacksoggetto)

    Thanks for being of help. Answered you there, but as I said, this was a recommendation, you are more than welcome to improve on my (bad) code

    Plugin Author Dennis Ploetner

    (@realloc)

    The code is in master now. I will prepare the next minor that should go up soon.

    Thanks for contributing!

    Thread Starter jacksoggetto

    (@jacksoggetto)

    Thanks for your work and for accepting my idea

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.