• Resolved jas

    (@jaskaranpreetsingh)


    Hi!

    Plugin is working as intended ie: its make auto post to Twitter when we publish post.

    But we are using https://permalinkmanager.pro/docs/filters-hooks/how-to-filter-the-default-custom-permalinks/

    For example if our post permalink is https://example.com/a-very-long-slug-with-seven-words/slug-with-exactly-five-words/another-pretty-long-sentence-that-should-be-shorten

    We trim it to make its length short https://example.com/a-very-long-slug-with-seven-words

    Below is code used for this purpose:

    function mp_limit_slugs_length($uri) {
        $max_words = 10; // If any part of URI contains more than 10 words, the slug will be limited to first 10 words
        $new_title = '';
        $slugs = explode('/', $uri);
    
    
        for($i=0, $count = count($slugs); $i < $count; $i++) {
            $slug = $slugs[$i];
            $words = explode('-', $slug);
    
            $new_title .= "/";
            if(count($words) > $max_words) {
                $new_title .= implode("-", array_slice($words, 0, $max_words));
            } else {
                $new_title .= $slug;
            }
        }
    
        // Remove trailing slashes
        $new_title = trim($new_title, "/");
    
        return $new_title;
    }
    add_filter('permalink_manager_filter_default_post_uri', 'mp_limit_slugs_length', 10);

    But problem is Twitter post uses the url which is saved while draft post ie: the long url but we want to use url which is created after wards ie: when post is published and url length is modified by using above code ie: filter permalink_manager_filter_default_post_uri

    Please suggest how we can make it possible.

    Thanks!

    • This topic was modified 1 year, 4 months ago by jas.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter jas

    (@jaskaranpreetsingh)

    I have added below code to make same thing to shorten length of url. Just writing to make sure if there is any filter/hook available.

    But still plugin do not use the final permalink of post.

    /*
     * Limit title length in permalink / slugs
     */
    
    add_action( 'save_post', 'wpse51363_save_post', 25, 2 );
    
    function wpse51363_save_post($post_id, $post){
    
        //if it is just a revision don't worry about it
        if (wp_is_post_revision($post_id))
            return;
    
        //if the post is an auto-draft we don't want to do anything either
        if($post->post_status != 'auto-draft' ){
    
            // unhook this function so it doesn't loop infinitely
            remove_action('save_post', 'wpse51363_save_post', 25 );
    
            //this is where it happens -- update the post and change the post_name/slug to the post_title
            $string = $_POST['textarea_title_field'];
            $words = explode(" ", $string);
            $firstTenWords = array_slice($words, 0, 10);
            $result = implode("-", $firstTenWords);
            $result = preg_replace('/[-–]+/', '-', $result);
            wp_update_post(array('ID' => $post_id, 'post_name' => str_replace(' ', '-', $result)));
    
            //re-hook this function
            add_action('save_post', 'wpse51363_save_post', 25 );
        }
    }

    • This reply was modified 1 year, 4 months ago by jas.

    We use get_permalink function of WordPress to get the permalink of post at transition_post_status hook which trigger on post status change.
    May be you should use filter hook like pre_post_link .
    But it would be better if you seek help from the permalinkmanager plugin support team .
    Hope this helps.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Incorrect Post permalink used in Tweet’ is closed to new replies.