WordPress.org

Ready to get started?Download WordPress

Forums

SubHeading
[resolved] Use SubHeading as default meta description (5 posts)

  1. ronald_123
    Member
    Posted 1 year ago #

    Firstly, thanks for this wonderful plugin.

    I want the subheading [Function: the_subheading()] for each and every post to be the default meta description for that post.

    I use WordPress SEO by Yoast.

    Please tell me what should be my course of action.

    Any help will be much appreciated.

    http://wordpress.org/extend/plugins/subheading/

  2. Steve
    Member
    Plugin Author

    Posted 1 year ago #

    Hi,

    Although unfamiliar with the WordPress SEO plugin in question, I have spotted that it makes use of a number of filters therefore it should be possible to do ask you require.

    Something like this may work to output the subheading as the meta description if one is set, otherwise use the default.

    add_filter( 'wpseo_metadesc', function( $description ) {
    	global $post;
    	if ( $subheading = get_post_meta( $post->ID, '_subheading', true ) ) {
    		return $subheading;
    	}
    	return $description;
    } );

    Note that I've not testing that it actually works.

    You could try adding this to your theme functions.php file and see if it makes any difference.

    Reference: See line 869 http://plugins.trac.wordpress.org/browser/wordpress-seo/trunk/frontend/class-frontend.php

    Hope this points you in the right direction.

  3. ronald_123
    Member
    Posted 1 year ago #

    This is causing an error in Google Chrome when I try to access the back end of the site:

    Server error
    The website encountered an error while retrieving http://www.beyondtheninetyminutes.com/wp-admin/theme-editor.php?file=functions.php&theme=Advanced-Newspaper&scrollto=400&updated=true. It may be down for maintenance or configured incorrectly.
    Here are some suggestions:
    Reload this webpage later.
    HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.

    Once I remove that coding, the site is working properly.

  4. Steve
    Member
    Plugin Author

    Posted 1 year ago #

    Hi,

    I've installed the WP SEO plugin mentioned and copied the snippet I wrote into the themes functions.php file (in this case TwentyTwelve), however it worked as expected for my tests.

    One small modification I would suggest is to remove any markup / tags using the following:

    add_filter( 'wpseo_metadesc', function( $description ) {
    	global $post;
    	if ( $subheading = get_post_meta( $post->ID, '_subheading', true ) ) {
    		return $subheading;
    	}
    	return $description;
    } );

    The issue you are having may be related to PHP versions, one thing I can suggest is removing the anonymous function used:

    function wpseo_metadesc_subheading( $description ) {
    	global $post;
    	if ( $subheading = get_post_meta( $post->ID, '_subheading', true ) ) {
    		return $subheading;
    	}
    	return $description;
    }
    add_filter( 'wpseo_metadesc', 'wpseo_metadesc_subheading' );

    As the theme functions file is included in the admin, any error introduced to the file will cause the 500 error you are seeing. With debugging and error outputting enabled it would probably show the actual error in more detail.

  5. ronald_123
    Member
    Posted 1 year ago #

    The second snippet worked like a charm. Thanks a lot!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic