-1

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?

  • 1
    If something changed with jetpack, doesn't it make more sense to ask them? – Mark Kaplun May 11 '16 at 7:26
  • In theory, yes and I am in the process of doing this. I also know this site isn't for support on plugins, but I'm trying to understand how it works so I have full control over future changes. I don't want to modify Jetpack code as there are so many plugin updates that I think it would get over-written so I need a solution outside of Jetpack, but I'm sure there's just an easy mistake in my code or I'm missing something obvious. – TomC May 11 '16 at 7:31
2

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.

  • Since posting, I tried $post->post_content; which still didn't work, but your suggestion: $content = get_post_field( 'post_content', $post->ID ); is working. Thanks – TomC May 11 '16 at 12:21
  • You can adjust the number of input arguments, like this: add_action( 'save_post', 'tsj_publicize_hashtags', 21, 3 ); to get access to e.g. $post_ID and $post and use function tsj_publicize_hashtags( $post_ID, $post, $update ) { @TomC – birgire May 11 '16 at 13:52
0

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' );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.