Making New Variables

  • Great plugin Guys!

    I want to ask is there a way I can add new variables to the list of available ones?

    Example: I would like to have a Variable for all Media Descriptions or Alt Text, so that this text can be used as a part of the Description or Title as needed (this could then be linked to the Featured Image per Page too).

    How could I do this?

Viewing 15 replies - 1 through 15 (of 20 total)
  • Hello Matthew,

    Thank you for your query.

    You can register a new variable. Please check this ticket for more information:

    https://support.rankmath.com/ticket/register-a-custom-template-variable/

    If you need help with anything specific in there, please let us know so we can help.

    Unfortunatly this link “https://www.facebook.com/groups/rankmathseopluginwordpress/permalink/358873651651634/” doesnt take me to the refering post.

    Would the below be correct, or are how should it work?

    add_action( 'rank_math/vars/register_extra_replacements', function() {
       RankMath\Helper::register_var_replacement( 'listingCat',array('RMPrimaryCategoryToListingCallback'),$args = array(
    	    'name'    => esc_html__( 'Featured Image Caption', 'rank-math' ),
    			'desc'    => esc_html__( 'Caption of the Pages Featured Image', 'rank-math' ),
    			'example' => esc_html__(GetFeaturedImageCaption())));
    	
    	
    } );
    
    function GetFeaturedImageCaption(){
    	
    	$post_id = $_GET['post'];
    	$thumbnail_id    = get_post_thumbnail_id($post->ID);
      $thumbnail_image = get_posts(array('p' => $thumbnail_id, 'post_type' => 'attachment'));
    	if ( !empty($thumbnail_image) ){
          $oTerm = get_term($thumbnail_image, 'p'); 
          if ( !empty($oTerm) && !is_wp_error($oTerm) ){
             return $oTerm->name;
          }
       }
    
    }

    Hi there,

    Thanks for the follow up.

    I do not quite follow the logic in your custom callback.
    Would you mind letting me know what you would like the function to return so that I can advise further?

    I am looking forward to helping you. Thank you.

    Ultimately I would like the add a %FeaturedImgCap% or similar as a variable which would pull the Caption value (that can be set in Media) of the Featured Image of for a Page.

    Does this make sense?

    Hi there,

    Thanks for the follow up.

    Please try the following code:

    add_action( 'rank_math/vars/register_extra_replacements', function() {
       RankMath\Helper::register_var_replacement( 'FeaturedImgCap',array('GetFeaturedImageCaption'),$args = array(
    	    'name'    => esc_html__( 'Featured Image Caption', 'rank-math' ),
    			'desc'    => esc_html__( 'Caption of the Pages Featured Image', 'rank-math' ),
    			'example' => esc_html__(GetFeaturedImageCaption())));
    	
    	
    } );
    
    function GetFeaturedImageCaption(){
    	
    	$post_id = $_GET['post'];
    	$thumbnail_id    = get_post_thumbnail_id($post->ID);
    	if($thumbnail_id){
        $attachment_meta = wp_get_attachment($thumbnail_id);
        return $attachment_meta['caption'];
        }
    
    }

    I hope this info helps. Thank you.

    So I’ve added the code in and it gives the option now in the dropdown menu, but it doesn’t show the Featured Images Caption.

    What else do I need to adjust?

    • This reply was modified 2 months, 3 weeks ago by  Matthew Wilding. Reason: Added it already

    Hello,

    Thank you for the follow up.

    I have made a few changes to the code, the below should do the trick:

    add_action( 'rank_math/vars/register_extra_replacements', 'register_my_replacements' );
    function register_my_replacements() {
    		RankMath\Helper::register_var_replacement(
    			'FeaturedImgCap','get_featured_image_cap',
    			array(
    				'name'    => esc_html__( 'Featured Image Caption.', 'rank-math' ),
    				'desc'    => esc_html__( 'Caption of the Pages Featured Image', 'rank-math' ),
    				'example' => get_featured_image_cap(),
    			)
    		);}
    	function get_featured_image_cap() {
        global $post;
        $post_id = $post->ID;
    		if($post_id==''){
    			$post_id = $_GET['post'];
    		}
    	$thumbnail_id = get_post_thumbnail_id($post_id);
    	$attachment = get_post($thumbnail_id);
    	
        $replacements = $attachment->post_excerpt;
    	
        return $replacements;
    	}

    I hope this helps. Thank you.

    ​​​​​​​

    Thanks this works great!

    One more question, I was thinking this would also work for Media objects but it doesn’t, as I guess the Media Item (image) isn’t featured in itself. How could I change this code to get the Caption of a Media item (image)?

    Thanks again for your help.

    HI Matthew,

    Thanks for the follow up.

    I am glad that the first solution works as for the second question, this will depend on how you are adding the image attachment, is it through a gallery block or perhaps a custom field?

    Here is some example code that you can use to get all attachments except the featured image from a post:

    if ( $post->post_type == 'data-design' && $post->post_status == 'publish' ) {
            $attachments = get_posts( array(
                'post_type' => 'attachment',
                'posts_per_page' => -1,
                'post_parent' => $post->ID,
                'exclude'     => get_post_thumbnail_id()
            ) );
     
            if ( $attachments ) {
                foreach ( $attachments as $attachment ) {
                    
                    //do something to $attachment
                }
                 
            }
        }

    I hope this info helps. Thank you.

    Thanks for this but what I was meaning was for the Media section so that the individual Media items can get this.

    Does that make sense?

    Hi Matthew,

    Thanks for the clarification.

    No this filter is designed for use on the Rank Math metabox, to be able to use the replacements on other areas I would suggest that you get in touch with your theme author so that they can advise further on this.

    I hope this info helps. Thank you.

    Sorry I am referring to a Rank Math metabox, see attached, it is just showing up as (%FeaturedImgCap%) not with the caption text.

    Media metadata Settings

    Hi Matthew,

    Yes this is expected behavior since the settings page does not have an image which the logic can load a caption from. But this should work on the single attachment page on the frontend. I have tested this on my end like so:
    IMG

    The source code looks like this:
    IMG

    I hope this info helps. Thank you.

    Ok I’m on the Media > Library page now but it’s showing like this:

    https://www.dropbox.com/s/34i9z1yj1fx2py7/Screenshot_20190527-224348.png?dl=0

    Is that correct too?

Viewing 15 replies - 1 through 15 (of 20 total)

The ticket ‘Making New Variables’ is closed to new replies.