• Resolved ChrisL

    (@chrslcy)


    I have a function which adds a post’s permalink as a custom field called ‘share_url’, like this:

    //
    // CREATE SHARE_URL CUSTOM FIELD - NON POSTIE
    //
     
    function create_share_url_customfield($post_id) {
    	global $post;
     	$post_url = get_permalink();
    	$post_url_sanitised = esc_url_raw($post_url);
    	if ($post->post_type == 'post'){
    		update_post_meta($post_id, 'share_url', $post_url_sanitised);
    	}
    }
    add_action( 'save_post', 'create_share_url_customfield' );

    It works well when posting via the admin area, but posts created via Postie Plugin are not updated. For that reason, I created a Postie function:

    //
    // CREATE SHARE_URL CUSTOM FIELD - POSTIE
    //
    
    add_filter('postie_post_before', 'add_share_url_custom_field');
    
    function add_share_url_custom_field($post) {
             $post_url = get_permalink($post['ID']);
    ? ?      add_post_meta($post['ID'], 'share_url', $post_url);
    ? ?      return $post;
    }

    This works, but the share_url takes the form of the ‘Ugly’ permalink (https://example.com/?p=N).?

    How do I ensure that the first function captures all posts, including those generated by Postie? Or, alternatively, ensure that the second Postie-specific function retrieves ‘Pretty Permalinks’?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Wayne Allen

    (@wayneallen-1)

    In your first function you rely on the the global $post variable. This doesn’t exist when Postie is processing messages.
    I would use $post_id to get the post via get_post().

    Thread Starter ChrisL

    (@chrslcy)

    That solved the problem. Thanks very much for the prompt.

    • This reply was modified 4 years, 5 months ago by ChrisL.
    Plugin Author Wayne Allen

    (@wayneallen-1)

    Excellent. You are very welcome.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Permalinks structure in Postie functions’ is closed to new replies.