admin管理员组文章数量:1427746
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?
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?
Share Improve this question edited May 11, 2016 at 7:38 TomC asked May 11, 2016 at 7:12 TomCTomC 1,3168 silver badges18 bronze badges 2- 1 If something changed with jetpack, doesn't it make more sense to ask them? – Mark Kaplun Commented May 11, 2016 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 Commented May 11, 2016 at 7:31
2 Answers
Reset to default 2Just 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.
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' );
}
本文标签: filtersCustomise Jetpack Publicize text
版权声明:本文标题:filters - Customise Jetpack Publicize text 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745502475a2661094.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论