Support

Account

Home Forums ACF PRO add hashtags automatically before posting Reply To: add hashtags automatically before posting

  • 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;
    }’