• Hello,
    I use the wpSEO plugin. And it offers the possibility to change the meta via a function.

    This is the function:

    add_action('init', 'set_wpseo_meta');
    
    function set_wpseo_meta() {
        apply_filters(
            'wpseo_set_meta',
            array(
                'title' => 'Neuer Titel',
                'desc' => 'Neue Description',
            )
        );
    }

    When I use it with static data it works well. No I like to have custom fields and taxonomies be used for creating the meta.

    This is how I get my dynamic meta.

    global $wp_query;
    
    $artists_list = wp_get_object_terms( $wp_query->post->ID, 'mhq_artists' );
    $label_list = wp_get_object_terms( $wp_query->post->ID, 'mhq_labels' );
    $cdTitle = get_post_meta( $wp_query->post->ID, 'my_meta_box_Title', true );
    $cdRelease = get_post_meta( $wp_query->post->ID, 'my_meta_box_dateRelease', true );
    
    foreach( $artists_list as $artists )
    $artist_names[] = $artists->name;
    
    foreach( $label_list as $labels )
    $label_names[] = $labels->name;
    
    $artist_names = implode( ', ', $artist_names );
    $label_names = implode( ', ', $label_names );

    Now, how do I get this dynamic into the function?

    When I try to change the variable with the static text I got an error.

    And, I like to have this all, only if(is_single()).

    Can someone tell me how this works?

    Cheers,
    Denis

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Use the ‘wpseo_set_meta’ filter like so:

    add_filter('wpseo_set_meta', 'dcgn_artist_meta');
    function dcgn_artist_meta() {
       //your code here
       return array(
                'title' => $title,
                'desc' => '$description',
            );
    }

    Your code’s task will be to distill all of your content down into $title and $description in a way that results in proper meta syntax once this makes its way out to the actual page. I’m not sure how artists and label names all get properly integrated into $description but that’s what you have to work with.

    Thread Starter DenisCGN

    (@deniscgn)

    Hey bcworkz!
    thanks for your answer. So I do not need this INIT thing?

    add_action('init', 'set_wpseo_meta');

    Where do I put the is_single() ? Around the complete add_filter ?

    What I try to do is…that I want to create a frame, like…

    “Here is our new CD-Review $albumTitle form $artist release by $label on $relasedate.”

    or something, that makes creating SEO a little bit easier for the editors.

    Cheers from Cologne,
    Denis

    Moderator bcworkz

    (@bcworkz)

    Hmm, I’m not too sure. I didn’t read you post carefully enough, I assumed the wrong initial state. Apologies.

    I’m a bit surprised your static code works, it’s sort of backwards from how things are usually done with filters. Now with a better understanding, you’re better off modifying the ‘init’ callback, adding your code above the apply_filters() and still distilling everything down to $title and $description, then supplying those to the apply_filters() array.

    add_action('init', 'set_wpseo_meta');
    
    function set_wpseo_meta() {
        if ( ! is_single() ) return;
        // your code here
        apply_filters(
            'wpseo_set_meta',
            array(
                'title' => $title,
                'desc' => $description,
            )
        );
    }

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom META in wpSEO plugin’ is closed to new replies.