Question

EDIT: Due to downvotes (with no comments!), I've edited this post to be a bit more specific...

Why isn't the following change working?

$custom_message = get_the_title( $post->ID ) .''. $hash_tags;

to:

$custom_message = get_the_content( $post->ID ) .' '. $hash_tags; 

Original Post

I'm trying to customise the text posted by Jetpack publicize to use the post content instead of the title as my posts don't have titles. This used to work previously, but something seems to have changed in Jetpack which now defaults to using the title and URL.

The code in the Jetpack publicize module is as follows:

function __construct() {
        $this->default_message = Publicize_Util::build_sprintf( array(
            /**
             * Filter the default Publicize message.
             *
             * @module publicize
             *
             * @since 2.0.0
             *
             * @param string $this->default_message Publicize's default message. Default is the post title.
             */
            apply_filters( 'wpas_default_message', $this->default_message ),
            'title',
            'url',
        ) );

        $this->default_prefix = Publicize_Util::build_sprintf( array(
            /**
             * Filter the message prepended to the Publicize custom message.
             *
             * @module publicize
             *
             * @since 2.0.0
             *
             * @param string $this->default_prefix String prepended to the Publicize custom message.
             */
            apply_filters( 'wpas_default_prefix', $this->default_prefix ),
            'url',
        ) );

        $this->default_suffix = Publicize_Util::build_sprintf( array(
            /**
             * Filter the message appended to the Publicize custom message.
             *
             * @module publicize
             *
             * @since 2.0.0
             *
             * @param string $this->default_suffix String appended to the Publicize custom message.
             */
            apply_filters( 'wpas_default_suffix', $this->default_suffix ),
            'url',
        ) );

I was wondering if it would be possible to add get_the_content into the wpas_default_message but haven't managed to get this working.

I also tried to modify the code from another plugin (by Jeremy Herve) which adds the post tags as hashtags as follows by changing get_the_title to get_the_content but it doesn't work:

function tsj_publicize_hashtags() {
    $post = get_post();
    if ( ! empty( $post ) ) {

    /* Grab the tags of the post */
        $post_tags = get_the_tags( $post->ID );

    /* Append tags to custom message */
        if ( ! empty( $post_tags ) ) {

        /* Create list of tags with hashtags in front of them */
            $hash_tags = '';
            foreach( $post_tags as $tag ) {
                $hash_tags .= ' #' . $tag->name;
            }

        /* Create our custom message */
            // $custom_message = get_the_title( $post->ID ) .''. $hash_tags;
            $custom_message = get_the_content( $post->ID ) .' '. $hash_tags; 
            update_post_meta( $post->ID, '_wpas_mess', $custom_message );

        }
    }
}
/* Save that message */
function tsj_cust_pub_message_save() {
add_action( 'save_post', 'tsj_publicize_hashtags', 21 );
}

add_action( 'publish_post', 'tsj_cust_pub_message_save' );
}

Can anyone point me in the right direction please on how to get the post content to be used in the publicize text?

Was it helpful?

Solution

Just some general remarks:

Why isn't the following change working?

    $custom_message = get_the_title( $post->ID ) .''. $hash_tags;

to:

    $custom_message = get_the_content( $post->ID ) .' '. $hash_tags;

Note that get_the_content() doesn't take a post ID as an input parameter and it depends on global variables, like $pages that's derived from $post->post_content (with some pagination adjustments), within the WP_Query::setup_postdata() method.

This is how it's defined:

function get_the_content( $more_link_text = null, $strip_teaser = false ) {
    global $page, $more, $preview, $pages, $multipage;
    ...
}

A starting point could be use

$post->post_content

if you have access to the $post object or

get_post_field( 'post_content', $post_ID );

if you only have the post ID and go from there.

Note that if you need to use the save_post hook, you can get the post ID and post object from the callback:

do_action( 'save_post', $post_ID, $post, $update );

so you don't need to use get_post().

PS: I wonder if you can use this hook in JetPack (just skimmed through it):

do_action( 'publicize_save_meta', $submit_post, $post_id, $service_name, $connection );

but it seems to be fired for each services/connections.

OTHER TIPS

Complete code (thanks to @birgire) in case anyone else finds useful in the future:

// Append Tags as Hashtags to Jetpack publicise posts
if ( class_exists( 'Jetpack' ) && Jetpack::is_module_active( 'publicize' ) ) {
    function tsj_publicize_hashtags() {
        $post = get_post();
        if ( ! empty( $post ) ) {
        /* Grab the tags and content of the post */
            $post_tags = get_the_tags( $post->ID );
            $content = get_post_field( 'post_content', $post->ID );
            //$content = $post->post_content;

        /* Append tags to custom message */
            if ( ! empty( $post_tags ) ) {
            /* Create list of tags with hashtags in front of them */
                $hash_tags = '';
                foreach( $post_tags as $tag ) {
                    $hash_tags .= ' #' . $tag->name;
                }
            /* Create our custom message using content instead of title */
                $custom_message = $content . ' ' . $hash_tags; 
                update_post_meta( $post->ID, '_wpas_mess', $custom_message );
            }
        }
    }
    /* Save that message */
    function tsj_cust_pub_message_save() {
    add_action( 'save_post', 'tsj_publicize_hashtags', 21 );
    }
    add_action( 'publish_post', 'tsj_cust_pub_message_save' );
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top