• Resolved sandeepjainlive

    (@sandeepjainlive)


    With action wp_after_insert_post i am trying to get

    $title = get_post_meta( $id, ‘_yoast_wpseo_title’, true );

    $description = get_post_meta( $id, ‘_yoast_wpseo_metadesc’, true );

    But it is getting as empty

    I checked in db meta are inserting

    this is function call

    add_action( ‘wp_after_insert_post’, array( $this, ‘send_notification_on_publish’ ), 10, 4 );

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter sandeepjainlive

    (@sandeepjainlive)

    This is whole code

    <?php
    /**
     * Send_Notification class for sending notifications on post publish.
     *
     * @package Pushrocket
     */
    
    namespace Pushrocket;
    
    /**
     * Send_Notification class for sending notifications on post publish.
     */
    class Send_Notification {
    	/**
    	 * Constructor for the Send_Notification class.
    	 */
    	public function __construct() {
    		add_action( 'wp_after_insert_post', array( $this, 'send_notification_on_publish' ), 10, 4 );
    	}
    	/**
    	 * Sends a notification when a post or web story is published.
    	 *
    	 * @param int     $id          The ID of the post being published.
    	 * @param WP_Post $post        The post object being published.
    	 * @param bool    $update      Whether this is an update to an existing post.
    	 * @param WP_Post $post_before The post object before the update.
    	 */
    	public function send_notification_on_publish( $id, $post, $update, $post_before ) {
    		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
    			return;
    		}
    		$show_multiple_site = get_transient( 'pushrocket_show_multiple_site' );
    		if ( 'yes' !== $show_multiple_site ) {
    			return;
    		}
    		$post_type = $post->post_type;
    		if ( 'publish' !== $post->post_status || ( $post_before && 'publish' === $post_before->post_status ) || wp_is_post_revision( $id ) ) {
    			return;
    		}
    		if ( 'post' === $post_type || 'web-story' === $post_type ) {
    			if ( $post->post_date === $post->post_modified ) {
    				$pushrocket_panel_url     = get_option( 'pushrocket_panel_url' );
    				$pushrocket_website_code  = get_option( 'pushrocket_website_code' );
    				$pushrocket_username      = get_option( 'pushrocket_username' );
    				$pushrocket_password      = get_option( 'pushrocket_password' );
    				$pushrocket_website_lists = get_option( 'pushrocket_website_lists' );
    				$website_lists            = count( $pushrocket_website_lists ) > 0 ? implode( ',', $pushrocket_website_lists ) : '';
    				if ( 'post' === $post_type ) {
    					$pushrocket_push_on_publish = get_option( 'pushrocket_push_on_publish' );
    					if ( 1 !== (int) $pushrocket_push_on_publish ) {
    						return false;
    					}
    				}
    				if ( 'web-story' === $post_type ) {
    					$pushrocket_push_on_publish_for_webstories = get_option( 'pushrocket_push_on_publish_for_webstories' );
    					if ( 1 !== (int) $pushrocket_push_on_publish_for_webstories ) {
    						return false;
    					}
    				}
    
    				if ( class_exists( 'WPSEO_Options' ) ) {
    					$title       = get_post_meta( $id, '_yoast_wpseo_title', true );
    					$description = get_post_meta( $id, '_yoast_wpseo_metadesc', true );
    				} elseif ( class_exists( 'RankMath' ) ) {
    					$title       = get_post_meta( $id, 'rank_math_title', true );
    					$description = get_post_meta( $id, 'rank_math_description', true );
    				}
    				if ( 'web-story' === $post_type ) {
    					$title       = get_the_title( $id );
    					$description = get_post_field( 'post_excerpt', $id );
    				}
    				if ( empty( $title ) ) {
    					$title = $post->post_title;
    				}
    				if ( empty( $description ) ) {
    					$description = $post->post_content;
    					$description = apply_filters( 'the_content', $post->post_content );
    				}
    					$img_url      = get_the_post_thumbnail_url( $id, 'large' );
    					$data_to_send = array(
    						'WebsiteURL'      => $pushrocket_panel_url,
    						'WebsiteCode'     => $pushrocket_website_code,
    						'UserName'        => $pushrocket_username,
    						'Password'        => $pushrocket_password,
    						'WebsiteList'     => $website_lists,
    						'MetaTitle'       => $title,
    						'MetaDescription' => $description,
    						'ImageURL'        => $img_url,
    						'PostURL'         => get_the_permalink( $id ),
    						'PostType'        => $post_type,
    					);
    			}
    		} else {
    			return false;
    		}
    	}
    }
    
    new Send_Notification();
    
    
    Plugin Support Maybellyne

    (@maybellyne)

    Hello @sandeepjainlive

    Thanks for using the Yoast SEO plugin. I understand you are using the wp_after_insert_post function to get the title and meta description.

    Sadly, this is beyond the scope of our support. If you have issues with using the features provided in the plugin UI, we’ll be happy to answer. The support team is here to help you with questions about the setup and basic use of the plugin. As much as we’d love to help, is beyond what we can do for our users.

    Thread Starter sandeepjainlive

    (@sandeepjainlive)

    Hi

    But here question for meta title and description from yoast with _yoast_wpseo_title,_yoast_wpseo_metadesc so i think you must provide support for it

    As i am already getting meta title and description with rank math plugin with rank_math_title,rank_math_description

    Thread Starter sandeepjainlive

    (@sandeepjainlive)

    Any update on it

    Plugin Support Suwash

    (@suascat_wp)

    @sandeepjainlive

    The best way to get data from Yoast SEO is to use the provided APIs like the?Yoast Surfaces API?and the?Yoast Schema API.

    For example, this code provides the page’s title as a variable:

    $title = YoastSEO()->meta->for_current_page()->title;

    This code immediately outputs the page’s meta description:

    echo YoastSEO()->meta->for_current_page()->description;

    We hope this helps.

    Thread Starter sandeepjainlive

    (@sandeepjainlive)

    @suascat_wp i checked with above function and in response to

    YoastSEO()->meta->for_current_page() getting below data

    all current dataYoast\WP\SEO\Surfaces\Values\Meta Object
    (
        [context] => Yoast\WP\SEO\Context\Meta_Tags_Context Object
            (
                [indexable] => Yoast\WP\SEO\Models\Indexable Object
                    (
                        [object_type] => unknown
                        [post_status] => unindexed
                        [version] => 1
                    )
    
                [presentation] => Yoast\WP\SEO\Presentations\Indexable_Presentation Object
                    (
                        [model] => Yoast\WP\SEO\Models\Indexable Object
                            (
                                [object_type] => unknown
                                [post_status] => unindexed
                                [version] => 1
                            )
    
                        [context] => Yoast\WP\SEO\Context\Meta_Tags_Context Object
     *RECURSION*
                    )
    
            )
    
    )

    In response to $title = YoastSEO()->meta->for_current_page()->title; sitename is coming and in response to ?YoastSEO()->meta->for_current_page()->description; empty is coming

    which is wrong

    Thread Starter sandeepjainlive

    (@sandeepjainlive)

    Now issue fixed

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Meta description and title not get with action wp_after_insert_post’ is closed to new replies.