• I post a lot using Press This and I allow other users to post. Lately I noticed some duplicates and I m trying to find a way to stop this. I am trying to get the first link from the post, add it to a custom field which will be checked on each publish_post and if it exists add the user’s name in another custom field.

    I did one test and the url was added to the “link” custom field and that s it. It stopped working. I m not sure what I m doing wrong.

    add_action( 'publish_post', 'check_post' );
    function check_post( $post_id ) {
    $user_info = get_userdata(1);
    
            function get_first_link() {
    
                global $post, $posts;
                preg_match_all('/href\s*=\s*[\"\']([^\"\']+)/', $post->post_content, $links);
                return $links[1][0];
    
            }
            $first_link = get_first_link();
    
            add_post_meta($post_id, 'link', $first_link, true);
            add_post_meta($post_id, 'users', $user_info->user_login, true);
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Using add_post_meta() with the last parameter set to true means if the key exists, the value you are trying to insert will fail. If set to false, it will store multiple values under the same key. I’m guessing neither is what you want.

    Consider using update_post_meta(). It will replace any value(s) with the one you provide. It will create a key if it does not exist.

    Thread Starter cip6791

    (@cip6791)

    Thank you, but I am using this when posting a brand new post. The link custom field doesn’t exist.

    The get_first_link function is not working for some reason. I m not sure how to debug … so I tried setting the $first_link to var_dump(get_first_link()); and it returns a NULL and an error. So I m guessing that s the problem.

    function get_first_link() {
    
                global $post, $posts;
                preg_match_all('/href\s*=\s*[\"\']([^\"\']+)/', $post->post_content, $links);
                return $links[1][0];
    
            }
            $first_link = get_first_link();
    Thread Starter cip6791

    (@cip6791)

    Ok … so this works but not when publishing a post. Only when updating it. What do I need to use instead? I tried publish_post and wp_publish_post.

    add_action( 'save_post', 'check_post' );
    function check_post( $post_id ) {
    
                global $post, $posts;
                preg_match_all('/href\s*=\s*[\"\']([^\"\']+)/', $post->post_content, $links);
    
    		$user_info = get_userdata(1);
    		$first_link = $links[1][0];
    if ( !wp_is_post_revision( $post_id ) ) {
    		update_post_meta($post_id, 'link', $first_link);
            	update_post_meta($post_id, 'users', $user_info->user_login);
    	}
    }
    Moderator bcworkz

    (@bcworkz)

    ‘save_post’ should fire on new all posts, as would ‘wp_insert_post’. If you want to avoid actions related to updates, use the variable action “{$old_status}_to_{$new_status}”. You can temporarily use the action ‘transition_post_status’ to determine the correct status values. While it doesn’t make sense there would be a transition for a new post, it fires at almost the same time as the other actions I mentioned on new post insertion.

    I think there may be a problem with the globals not being yet defined for new posts. However, $post is passed as a second or third parameter in the above actions. Just add the priority and parameter count parameters to your add_action() call and then add the $post parameter to your function definition to access it.

    add_action( 'save_post', 'check_post', 10, 2 );
    function check_post( $post_id, $post ) {
      preg_match_all( // blah blah etc....

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Add first url to custom field on publish_post’ is closed to new replies.