WordPress.org

Forums

Custom Permalinks
[resolved] Default URL not equal wp permanent link configurations (1 post)

  1. leomuniz
    Member
    Posted 12 months ago #

    I had this problem, solved, and probably someone may need this code to answer the same problem.

    After installing this plugin, my new posts were no longer obeying the rule stablished at Permanent Links Configuration page: http://site.com/blog/%category%/%postname%/

    I was getting the default wordpress url style: ?p=1234

    To solve this problem, I modified the function custom_permalink_get_sample_permalink_html at custom-permalinks.php file, line 288. See it bellow:

    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);
    
            // I commented this block of code that used to create the input text box
    	/*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);
            }
    
        // Added this if to ensure the substitution of "no-category" category on permalink ("sem-categoria" is in portuguese, ATTENTION to use your wp language here)
            if ( strpos($permalink, "sem-categoria") ) {
    	        $categories = get_the_category($post->ID);
    	        $firstCategory = count($categories) - 1;
    	        $catSlug = $categories[$firstCategory]->slug;
    			$permalink = str_replace("sem-categoria", $catSlug, $permalink);
            }
        }
    
            // Here I detect the correct permalink and erase the site_url from it
    	$slugURL = str_replace(site_url()."/","",$permalink);
    
            // Here I pasted the block of code that I have commented before, changing the $permalink variable by the new $slugURL variable that I created before
    	ob_start();
    	custom_permalinks_form($slugURL, ($post->post_type == "page" ? custom_permalinks_original_page_link($id) : custom_permalinks_original_post_link($id)), false);
    	$content = ob_get_contents();
    	ob_end_clean();
    
    	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" : "" );
    }

    Hope this helps someone.
    Be my guest if you have some doubts. =D

    https://wordpress.org/plugins/custom-permalinks/

Reply

You must log in to post.

About this Plugin

  • Custom Permalinks
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic