Support

Account

Forum Replies Created

  • A temporary workaround I am using to prevent a user from adding text to a field which get its data generated by the server.

    add_action('admin_head', 'my_custom_css');
    function my_custom_css() {
      echo '<style>';
               .field_key-field_534dc7420db07 {display:none;}
            </style>';
    }
  • Found a temporary solution, not sure if the postdivrich is the best choice but it works.

    add_action('admin_head', 'my_custom_css');
    function my_custom_css() {
      echo '<style>
              #postdivrich {
                      background-color:white; padding:10px 10px 10px 10px;
                      font-size: 16px; line-height: 1.5em; font-weight: bold;
              }
              #postdivrich::before {
                      content: "Post Main Body";
              }
            </style>';
    }
  • Yes acf/save_post runs whenever the post is saved, even as a draft. Because of the nature of $value I cannot just assign a value to every post (lots of spam posts), only those that are accepted for Publish (the $value is a limited id that is taken off a finite stack, so only posts that are Published should be assigned one).

    I cannot check if the field is empty, because ACF does not have a function to hide fields, if the user was to enter anything into the field box, an actual $value would not be assigned.

    I did however find a way to hide the field with a custom css, so that a user cannot enter anything into it (unless they do some css hacking). I then can use the acf/save_post, and if the status is “Publish” AND the field is empty, then I assign a $value.

    This is just a workaround, and ideally ACF should work with:

    function get_doi( $new_status, $old_status, $post ) {
        if ( $new_status == 'publish' && $old_status != 'publish' ) {
    	$value = "12345";
    	update_field( "field_534dc7420db07", $value, $post->ID );
        }
    }
    add_action('transition_post_status', 'get_doi', 10000, 3 );

    I believe it is a timing issue, as ACF is set to trigger with priority 10. So I set the get_doi to trigger with priority 10000, so it should trigger after ACF saves, but I does not.

  • Hooking the acf/save_post wont work, since every time the post is updated after it is published the acf/save_post will trigger and replace the original $value with a new $value.

    Consider $value being a unique timestamp that is run only once when the post transitions to the published state. $value = time();

    If this is my functions.php, when the already Published post is updated field_534dc7420db07 gets a new value.

    add_action('acf/save_post', 'my_acf_save_post', 20);
    function my_acf_save_post( $post_id ){
        $stat = get_post_status( $post_id );
        $value = time();
        update_field('field_534dc7420db07', $value, $post_id);
    }

    I can check the status of the post in my_acf_save_post, but only the current value, not if it is transitioning from pending to publish.

    This is the reason I need to, and should, run the transition_post_status.

  • I found that if I comment out line 61 of acf.php it works (however all my other Custom Fields do not get saved).
    //add_action('acf/save_post', array($this, 'save_post'), 10);

  • I switched it all to:

    function your_callback( $post ) {
            $value = "12345";
            update_field( "field_534dc7420db07", $value, $post->ID );
    }
    add_action( 'pending_to_publish', 'your_callback' );

    but it has the same effect as before. The current post field gets blanked out, but if I use postid-1 it works fine.

  • Right when the post is Published by the Administrator, I need to generate a unique ID for that post (this is done by the server). So the Contributor does not (and shouldn’t) enter anything.

    Isn’t the action private_to_published basically the same as transition_post_status?

    PS I also just realized WordPress has recently been auto-updated, not sure if this is causing it.

  • I figured it out. I need to use $field_key instead of $field_name.
    update_field( "field_53362544f0012", $value, $post->ID );

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