Support

Account

Home Forums ACF PRO add hashtags automatically before posting

Solved

add hashtags automatically before posting

  • I have a frontend form which collects a whole bunch of data. It then calls

    add_action(‘acf/save_post’, ‘my_post_updater’, 20);

    And this gets some data from ACF to change the title and so on and finally calls

    wp_update_post( $my_post );

    to update the post. The post appears with the correct title, content, etc.

    I also use Jetpack publicize. New posts made on the front end are automatically posted on Facebook and Twitter with the newly created title.

    However, I’m trying to work out how to add hashtags to Jetpack publicize automatically. All the solutions I’ve found are being ignored I *think* because the code (in functions.php) is being ignored.

    Has anyone any ideas how I can sort this?

  • Hi @arathra
    Where in the post are you trying to add hashtags?

  • I don’t want them added in the post per se, just to the Jetpack publicise plugin. Apparently I can do this through a small function but somehow ACF and the code don’t go together.

    When using ACF the title is generated automatically; I use add_action(โ€˜acf/save_postโ€™, โ€˜my_post_updaterโ€™, 20); to change that title.

    This new title is publicised by Jetpack but WITHOUT hashtags. I need to add hashtags to it.

    Just wondering if you’ve come across this issue before and how to do it.

  • Unfortunately no, I don’t usually use jetpacks functions ๐Ÿ™‚

    But if you post your code I could help you and see if there’s anything obvious you’re missing.

  • This is my functions.php with the two functions. The first to change the title and info in the post; the second to the add the tags… which doesn’t work for some reason.

    I appreciate anything you might suggest here!

    `//Auto add and update Title field:
    function my_post_updater( $post_id ) {

    // unhook this function to prevent infinite looping
    remove_action( ‘acf/save_post’, ‘my_post_updater’, 20 );

    // change status if admin is user
    if ( um_user( “role” ) == ‘admin’ ) {
    $pStatus = “publish”;
    } else {
    $pStatus = “draft”;
    }

    // some variables to use
    $pLocation = $_POST[‘acf’][‘field_554f18ecbf60c’];
    $pLocation = strtoupper( $pLocation );

    // get some tags of location + tags
    $pTags = $pLocation;
    $also_casting_in = $_POST[‘acf’][‘field_55c9ee7857c82’];
    foreach( $also_casting_in as $value ) {
    $pTags = $pTags . “, ” . $value;
    }
    $pTags = strtolower( $pTags );

    // get the role & location & build the title
    $pTitle = $pLocation . “: ” . $_POST[‘acf’][‘field_559918d8fce21’];

    // add the info at the end if it’s locale_get_default
    $extrainfo = “”;
    if ( $_POST[‘acf’][‘field_554f3827eee0c’] == 1 ) {
    $extrainfo = “[L]”;
    }
    if ( $_POST[‘acf’][‘field_55bf6059da1d8’] == “P” ) {
    if ( $extrainfo == “[L]” ) {
    $extrainfo = “[L,P]”;
    } else {
    $extrainfo = “[P]”;
    }
    }
    if ( $_POST[‘acf’][‘field_55bf6059da1d8’] == “NP” ) {
    if ( $extrainfo == “[L]” ) {
    $extrainfo = “[L,NP]”;
    } else {
    $extrainfo = “[NP]”;
    }
    }
    if ( $extrainfo != “” ) {
    $pTitle = $pTitle . ” ” . $extrainfo;
    }

    // add the custom fields box onto the main content
    $pContent = $_POST[‘acf’][‘field_55991581f79f7’] . “<p>[Custom Fields]</p>”;

    // Create the update
    $my_post = array();
    $my_post[‘ID’] = $post_id;
    $my_post[‘post_title’] = $pTitle;
    $my_post[‘post_status’] = $pStatus;
    $my_post[‘post_content’] = $pContent;
    $my_post[‘tags_input’] = $pTags;

    // the permalink bit; begin by getting the slug
    $post = get_post($post_id);
    $slug = $post->post_name;
    if ( is_numeric ( $slug ) ) {
    // change to something more tangible otherwise leave alone
    $my_post[‘post_name’] = $pTitle;
    }

    // Update the post into the database
    wp_update_post( $my_post );

    // send quick email to tell me
    $pTitle = “New Post: ” . $pTitle;
    $pContent = do_shortcode( $pContent );
    $headers = array( ‘Content-Type: text/html; charset=UTF-8’ );
    wp_mail( “xxxx@xxxx.xxx”, $pTitle, $pContent, $headers );

    // rehook this function to prevent infinite looping
    add_action( ‘acf/save_post’, ‘my_post_updater’ );

    }
    add_action(‘acf/save_post’, ‘my_post_updater’, 20);

    // adds some hashtags to posts to be publicized by jetpack
    add_filter( ‘wpas_default_suffix’, ‘add_default_publicize_hashtag_suffix’, 10, 4 );
    function add_default_publicize_hashtag_suffix() {
    $default_tags = ‘ #castingcall #audition #actor’;
    return $default_tags;
    }’

  • Hi,

    Could you edit this (or repost) the code in code-tags so it’s readable? ๐Ÿ˜‰

  • Oh, Sorry, I thought I did! Here it is ๐Ÿ™‚

    <?php
    
    //Auto add and update Title field:
    function my_post_updater( $post_id ) {
    
    	// unhook this function to prevent infinite looping
        remove_action( 'acf/save_post', 'my_post_updater', 20 );
    	
    	// change status if admin is user
    	if ( um_user( "role" ) == 'admin' ) { 
    		$pStatus = "publish";
    	} else {
    		$pStatus = "draft";
    	}
    
    	// some variables to use
    	$pLocation = $_POST['acf']['field_554f18ecbf60c'];	
    	$pLocation = strtoupper( $pLocation );
    
    	// get some tags of location + tags
    	$pTags = $pLocation;
    	$also_casting_in = $_POST['acf']['field_55c9ee7857c82'];
    	foreach( $also_casting_in as $value ) {
    		$pTags = $pTags . ", " . $value;
    	}
    	$pTags = strtolower( $pTags );
    
    	// get the role & location & build the title
    	$pTitle = $pLocation . ": " . $_POST['acf']['field_559918d8fce21'];
    
    	// add the info at the end if it's locale_get_default
    	$extrainfo = "";
    	if ( $_POST['acf']['field_554f3827eee0c'] == 1 ) {
    		$extrainfo = "[L]";
    	} 
    	if ( $_POST['acf']['field_55bf6059da1d8'] == "P" ) {
    		if ( $extrainfo == "[L]" ) {
    			$extrainfo = "[L,P]";
    		} else {
    			$extrainfo = "[P]";
    		}
    	} 
    	if ( $_POST['acf']['field_55bf6059da1d8'] == "NP" ) {
    		if ( $extrainfo == "[L]" ) {
    			$extrainfo = "[L,NP]";
    		} else {
    			$extrainfo = "[NP]";
    		}
    	}
    	if ( $extrainfo != "" ) {
    		$pTitle = $pTitle . " " . $extrainfo;
    	}
    		
    	// add the custom fields box onto the main content
    	$pContent = $_POST['acf']['field_55991581f79f7'] . "<p>[Custom Fields]</p>";
    
    	// Create the update
    	$my_post = array();
    	$my_post['ID'] = $post_id;
    	$my_post['post_title'] = $pTitle;
    	$my_post['post_status'] = $pStatus;
    	$my_post['post_content'] = $pContent;
    	$my_post['tags_input'] = $pTags;
    
    	// the permalink bit; begin by getting the slug
    	//$post = get_post($post_id);
    	//$slug = $post->post_name;
    	//if ( is_numeric ( $slug ) ) {
    		// change to something more tangible otherwise leave alone
    		$my_post['post_name'] = $pTitle;
    	//}
    	
    	// Update the post into the database
    	wp_update_post( $my_post );
    	
    	// send quick email to tell me 
    	$pTitle = "New Post: " . $pTitle;
    	$pContent = do_shortcode( $pContent );
    	$headers = array( 'Content-Type: text/html; charset=UTF-8' );
    	wp_mail( "drama@encast.it", $pTitle, $pContent, $headers );
    
    	// rehook this function to prevent infinite looping
        add_action( 'acf/save_post', 'my_post_updater' );
    
    }
    add_action('acf/save_post', 'my_post_updater', 20);
    
    // adds some hashtags to posts to be publicized by jetpack
    add_filter( 'wpas_default_suffix', 'add_default_publicize_hashtag_suffix', 10, 4 );
    function add_default_publicize_hashtag_suffix() {
    	$default_tags = ' #castingcall #audition #actor';
    	return $default_tags;
    }
    
    ?>
  • Everything looks decent to me. There’s some code optimization I’d do but nothing that would change any functionality.

    I’ve read that some has a problem with it not working in functions.php but works in a standalone plugin. I also found this plugin that seems to do exactly what you want. Perhaps give that a try? ๐Ÿ˜‰
    https://wordpress.org/plugins/publicize-with-hashtags/

  • Thanks for that – I tried the plugin but it also didn’t work. I have a feeling I’m going to have to wait for Jetpack to update and allow this.

  • Alright ๐Ÿ™‚

    Yeah perhaps that’s the best way to go. You could also notify them of this in their own support forum so it might be pushed forward a bit.

    Best of luck!

  • I got there in the end.

    In the ACF function I had, I updated the post but BEFORE that I created the publicize message (including hashtags) and inserted that into the right post meta:

    // add the tags to the post title as $custom_message
    update_post_meta( $post_id, '_wpas_mess', $custom_message );
    	
    // Update the post into the database
    wp_update_post( $my_post );
    

    And it works now.

    Thanks for your help!

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.