• It seems when created a new post the title is defaulting to ?p=XXXX, even though the permalink structure is set to %postname% in the backend.

    One fix I came up with that seems to resolve it is by adding the following to custom_permalinks_get_sample_permalink_html():

    if(!empty($new_title)){
            $permalink = get_sample_permalink($post->ID, $new_title, $new_slug)[1];
        }

    Here is the full method:

    function custom_permalink_get_sample_permalink_html($html, $id, $new_title, $new_slug) {
    
        $permalink = get_post_meta( $id, 'custom_permalink', true );
        $post = &get_post($id);
    
        if(!empty($new_title)){
            $permalink = get_sample_permalink($post->ID, $new_title, $new_slug)[1];
        }
    
        ob_start();
        ?>
        <?php custom_permalinks_form($permalink, ($post->post_type == "page" ? custom_permalinks_original_page_link($id) : custom_permalinks_original_post_link($id)), false); ?>
        <?php
        $content = ob_get_contents();
        ob_end_clean();
    
        if ( 'publish' == $post->post_status ) {
            $view_post = 'page' == $post->post_type ? __('View Page') : __('View Post');
        }
    
        if ( preg_match("@view-post-btn.*?href='([^']+)'@s", $html, $matches) ) {
            $permalink = $matches[1];
        } else {
            list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
            if ( false !== strpos($permalink, '%postname%') || false !== strpos($permalink, '%pagename%') ) {
                $permalink = str_replace(array('%pagename%','%postname%'), $post_name, $permalink);
            }
        }
    
        return '<strong>' . __('Permalink:') . "</strong>\n" . $content .
        ( isset($view_post) ? "<span id='view-post-btn'><a href='$permalink' class='button' target='_blank'>$view_post</a></span>\n" : "" );
    }

    https://www.ads-software.com/plugins/custom-permalinks/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Does this happen with posts as well?

    I was about to use this plugin, but I noticed it has this glitch with pages.

    Any other bugs to be aware of, supposing your fix works? (and why aren’t these fixes added to the updates?)

    Thanks for the feedback, and for sharing this mate. ??

    I came up with a solution that fixes it without modifying the plugins files, add this to your theme functions.php

    /**
     * Hooks into Custom Permalinks to fix default
     */
    function fix_custom_permalink_get_sample_permalink_html($html, $id, $new_title, $new_slug) {
    	$permalink = get_post_meta( $id, 'custom_permalink', true );
    
    	if (!$permalink && !empty($new_title)) {
    		$permalink = get_sample_permalink($id, $new_title, $new_slug)[1];
    		$post = get_post($id);
    		if ($post->post_type) {
    			$config = get_post_type_object($post->post_type);
    			if (isset($config->rewrite) && !empty($config->rewrite['slug'])) {
    				$slug = $config->rewrite['slug'];
    			} else {
    				$slug = $post->post_type;
    			}
    			$permalink = $slug . '/' . $permalink;
    		}
    		add_post_meta($id, 'custom_permalink', $permalink );
    	}
    	return call_user_func_array('custom_permalink_get_sample_permalink_html', func_get_args());
    }
    add_action('init', function () {
    	remove_filter( 'get_sample_permalink_html', 'custom_permalink_get_sample_permalink_html', 'edit_files', 4 );
    	add_filter( 'get_sample_permalink_html', 'fix_custom_permalink_get_sample_permalink_html', 'edit_files', 4 );
    });

    I have the EXACT same problem. But unfortunately none of these fixes worked for me.
    And if it doesnt work with %postname% perma structure - then its completely useless.
    A great plugin, but with the lack attention of the author..

    Thank you, @aikar. That mostly worked for me, except that my version of PHP doesn’t allow array indexing of function returns, and it produced permalinks like “site.com/post/sample-post” where I just wanted “site.com/sample-post/”

    This shortened snippet in in my functions.php works for me:

    function fix_custom_permalink_get_sample_permalink_html($html, $id, $new_title, $new_slug) {
    
    	$permalink = get_post_meta( $id, 'custom_permalink', true );
    
    	if ( !$permalink && !empty($new_title) ) {
    		$permalink = get_sample_permalink($id, $new_title, $new_slug);
    		add_post_meta( $id, 'custom_permalink', $permalink[1] . '/' );
    	}
    
    	return call_user_func_array('custom_permalink_get_sample_permalink_html', func_get_args());
    
    }
    add_action('init', function () {
    	remove_filter( 'get_sample_permalink_html', 'custom_permalink_get_sample_permalink_html', 'edit_files', 4 );
    	add_filter( 'get_sample_permalink_html', 'fix_custom_permalink_get_sample_permalink_html', 'edit_files', 4 );
    });

    I found that, using the last solution I posted, it was creating new “custom permalinks” for each post even if I was just using the default permalink, which for me is %postname%.

    I think the problem is related to the plugin trying to get the default or original permalink before the draft has auto-saved. Perhaps, in that case, it’s just returning the plain permalink because there is no title to generate a slug. Again, just guessing at this.

    I found that discarding my above solution and applying the following edits to the plugin code will make it work as expected. In the custom-permalinks.php file, look for line 297 and replace these three lines:

    ob_start();
      ?>
      <?php custom_permalinks_form($permalink, ($post->post_type == "page" ? custom_permalinks_original_page_link($id) : custom_permalinks_original_post_link($id)), false); ?>

    with these lines:

    $original = $post->post_type == "page" ? custom_permalinks_original_page_link($id) : custom_permalinks_original_post_link($id);
    
      if ( $permalink == '' && !empty($new_title) ) {
        $original = get_sample_permalink( $post->ID, $new_title, $new_slug );
        $original = $original[1];
      }
    
      ob_start();
      ?>
      <?php custom_permalinks_form($permalink, $original, false); ?>

    This will supply the correct default permalink for %postname% permalinks without creating new custom permalinks (unless you manually edit it). At least, it works in my testing.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Incorrect Default Permalink’ is closed to new replies.