Custom Post Types – 404 error when updating/saving posts

Home Forums BulletProof Security Pro Custom Post Types – 404 error when updating/saving posts

This topic contains 29 replies, has 2 voices, and was last updated by  AITpro Admin 1 year, 9 months ago.

Viewing 15 posts - 1 through 15 (of 30 total)
  • Author
    Posts
  • #30761

    Immerse
    Participant

    I installed bps pro today, replacing an alternative plugin. I have looked at the troubleshooting tips and can’t see anything that relates to this, so here goes…

    I managed to get it added in the end, and added only the custom code to stop username enumeration. Otherwise it was more or less left to sort itself out. Once the plugins were all allowed to play, the security log was clear. It still is.

    If I now go into my custom post types, open a post and edit it, then save/update the post, wordpress now takes something like 20 seconds to think about it, then triggers a 404 error. The error is for https://www.domain.co.uk/folder/wp-admin/post.php [example URL – not valid]

    Nothing shows up, by way of errors or in logs. Is this possibly related to bps? I’ve never seen it before and it happens every time I try to update a post!

    #30762

    AITpro Admin
    Keymaster

    Is the Custom Post Type something that you created using WP Custom Post Types methods here:  https://codex.wordpress.org/Post_Types#Custom_Post_Types or is this a plugin that is creating Custom Post Types?

    #30763

    AITpro Admin
    Keymaster

    I created a Custom Post Type using the exact WordPress example code in the link above and created a must-use plugin (custom-post-types.php).  I tested created a new Post, editing it and resaving it and did not see a 404 error, BUT when I try to view the Custom Post Type Post I see a 404 error:  http://demo9.local/acme_product/custom-post-types-test/. The reason I am seeing a 404 error is because the slug is not a valid URL in my site structure. See WP info below.  I assume you would need to do something with the permalink or probably creating an htaccess rewriterule would work.  I will test creating an htaccess rewriterule in a minute and post my findings.

    https://codex.wordpress.org/Post_Types#URLs

    URLs

    A custom post type will also get its own slug within the site URL structure. In the above example, a post of this product custom post type can be displayed at http://example.com/acme_product/%product_name% where acme_product is the slug of your custom post type and %product_name% is the slug of your particular product, so a permalink could be e.g.http://example.com/product/foobrozinator. You can see this permalink appear on the edit post screen for your custom post type, just like with default post types.

    #30764

    AITpro Admin
    Keymaster

    Yep, you can create htaccess RewriteRules to handle the 404 error by creating htaccess Custom Post Types RewriteRules.  See the example code shown below.

    1. Go to the BPS htaccess File Editor page, click on the Your Current Root htaccess File tab, scroll down in your Root .htaccess file code until you see this .htaccess code below.

    # WP REWRITE LOOP START
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]

    2. Copy your # WP REWRITE LOOP START code to this BPS Root Custom Code text box:  CUSTOM CODE WP REWRITE LOOP START
    3. After you have copied your WP Rewrite Loop Start .htaccess code to Custom Code add your Custom Post Types RewriteRules code.  Your code should look like this example below.
    4. Click the Save Root Custom Code button.
    5. Go to the BPS Security Modes page and click the Root Folder BulletProof Mode Activate button.

    # WP REWRITE LOOP START
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    
    # Custom Post Types RewriteRules
    RewriteRule ^acme_product/ - [L]
    RewriteRule ^another_product/ - [L]
    RewriteRule ^some_other_product/ - [L]
    #30765

    Immerse
    Participant

    The CPT was created via a simple plugin I created.  The fields for each CPT were created via ‘ACF’ plugin.  The data is submitted front-end by a ‘Formidable Forms’ form.  Upon submission, the post is set to draft status. The title and url are then generated via code in functions.php when the post is approved and published. I know that when you save/update a post it passes thru the code in functions.php (to set the title/url) because if you change anything it will change the title or url. I was wondering now if this had any bearing – hope not because the site is a directory site which requires front-end submission (and editing) of these posts.

    To register a cpt this is in a simple plugin:

    // register custom post type for catteries
    function bcats_create_post_type() {
    // set up labels
    $labels = array(
    'name' => 'Catteries',
    'singular_name' => 'Cattery',
    'add_new' => 'Add New Cattery',
    'add_new_item' => 'Add New Cattery',
    'edit_item' => 'Edit Cattery',
    'new_item' => 'New Cattery',
    'all_items' => 'All Catteries',
    'view_item' => 'View Cattery',
    'search_items' => 'Search Cattery',
    'not_found' =>  'No Catteries Found',
    'not_found_in_trash' => 'No Catteries found in Trash',
    'parent_item_colon' => '',
    'menu_name' => 'Catteries',
    );
    //register post type
    register_post_type( 'Cattery', array(
    'labels' => $labels,
    'has_archive' => true,
    'public' => true,
    'supports' => array( 'title', 'editor', 'author', 'thumbnail'),
    'taxonomies' => array( 'cattcounty','cattarea','post_tag' ),
    'exclude_from_search' => false,
    'capability_type' => 'post',
    'rewrite' => array( 'slug' => 'cattery' ),
    )
    );
    }
    add_action( 'init', 'bcats_create_post_type' );
    // register custom post type for catteries - end
    
     
    
    There is also code to create the necessary taxonomies:
    
    // Register COUNTY TAXONOMY FOR Cattery Posts - Name cattcounty
    if ( ! function_exists( 'cattcounty_taxonomy' ) ) {
    function cattcounty_taxonomy() {
    $labels = array(
    'name'                       => 'CattCounties',
    'singular_name'              => 'CattCounty',
    'menu_name'                  => 'CattCounty',
    'all_items'                  => 'All CattCounties',
    'new_item_name'              => 'New CattCounty Name',
    'add_new_item'               => 'Add New CattCounty',
    'edit_item'                  => 'Edit CattCounty',
    'update_item'                => 'Update CattCounty',
    'view_item'                  => 'View CattCounty',
    'add_or_remove_items'        => 'Add or remove CattCounty',
    'items_list'                 => 'CattCounty list',
    'items_list_navigation'      => 'Items list navigation',
    );
    $args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
    'query_var'                  => true,
    'rewrite'                    => array( 'slug' => 'county-cattery' ),
    );
    register_taxonomy( 'cattcounty', array( 'cattery' ), $args );
    }
    add_action( 'init', 'cattcounty_taxonomy', 0 );
    }
    
     
    
    Once the post is saved, the title and url are created with this in functions.php
    // should create custom POST TITLE FOR CATTERY from name and postcode
    function custom_post_title_generator( $post_id ) {
    $my_post = array();
    $my_post['ID'] = $post_id;
    $cattname    = get_field('short_catteryname');
    $postal    = get_field('catt_full_postcode');
    $fullcattname  = get_field('full_catteryname');
    $slugtitle  = $cattname . ' - ' . $postal;
    $title     = $fullcattname;
    $slug    = sanitize_title( $slugtitle );
    if ( get_post_type() == 'cattery' ) {
    $my_post['post_title'] = $title;
    $my_post['post_name'] = $slug;
    }
    // unhook this function so it doesn't loop infinitely
    remove_action( 'acf/save_post', 'custom_post_title_generator', 20 );
    // Update the post into the database
    wp_update_post( $my_post );
    // added this line to help try make it save properly - not sure if it helps - only the line and not the bracket below
    add_action('acf/save_post', 'custom_post_title_generator', 20);
    }
    // run after ACF saves the $_POST['fields'] data
    add_action('acf/save_post', 'custom_post_title_generator', 20);
    // should create custom POST TITLE FOR CATTERY from name and postcode - END

    Now, I have no idea if any of this relates to what your plugin does with htaccess, obviously. In terms of plugins, I had to add 3 whitelist rules for ‘hidden plugins’ which were (1 post-types) the cpt creation plugin, (2 britcats-google etc) google analytics code and (3 britcats-plugin) another plugin which from memory was used to specify who could modify certain posts.

    The whitelist code (just copied and pasted from bps) is britcats-google-analytics.php, britcats-plugin.php, post-types.php. I can’t think what else might be useful. My hosting company just changed the memory limits to 512k from 256k but it hasn’t made any difference.

    #30766

    AITpro Admin
    Keymaster

    I already posted a solution. See my previous reply.  Or are you saying you already tried the solution I posted above and it did not work?

    #30769

    Immerse
    Participant

    I just looked in cpanel and can see hundreds of errors. I’ve edited the domain name and the server ip numbers. I’m sure many are repeats but they all seem so similar it’s hard to tell.

    [Sat Aug 27 16:02:08.559131 2016] [authz_core:error] [pid 3052] [client xx.xx.xx.xx:43547] AH01630: client denied by server configuration: /home/domainname/public_html/cats/wp-content/plugins/bulletproof-security/admin/mod-test/mod_authz_core-nc-denied.png, referer: https://www.domain.com/cats/wp-content/plugins/bulletproof-security/admin/mod-test/mod_authz_core-nc-denied.png
    [Sat Aug 27 16:02:06.502608 2016] [access_compat:error] [pid 28818] [client xx.xx.xx.xx:43544] AH01797: client denied by server configuration: /home/domainname/public_html/cats/wp-content/plugins/bulletproof-security/admin/mod-test/mod_access_compat-od-nc-denied.png, referer: https://www.domain.com/cats/wp-content/plugins/bulletproof-security/admin/mod-test/mod_access_compat-od-nc-denied.png
    #30770

    Immerse
    Participant

    Your answers didn’t show up until after I posted my last, as I had kept this page open. I will read them now.

    #30771

    AITpro Admin
    Keymaster

    You can disregard those server log entries.  They are related to the BPS Apache Module testing script.  They are supposed to be errors to verify what htaccess code does and does not work on your particular server.  Nothing and nothing less – not a problem.

    I tested your CPT code that you posted above and it does not work with the RewriteRules htaccess code that did work when I tested the WordPress CPT example code from the WordPress site.  So either there is a problem with your CPT code or however your CPT code is doing things will require a different solution.  I am testing things now.

    #30772

    Immerse
    Participant

    Okay, read the first reply you offered, with

    # Custom Post Types RewriteRules
    RewriteRule ^acme_product/ - [L]
    RewriteRule ^another_product/ - [L]
    RewriteRule ^some_other_product/ - [L]
    
    Firstly, I assume I replace acme_product with them name of my cpts?
    
    Secondly, the posts are showing fine in front end. That has not been a problem. It is when I save in admin, after editing, that instead of coming back to the edit post page in admin, I get a 404. Does your solution relate to that, or did you think the posts weren't displaying front end?
    
    The 404 I get is when wordpress tries to save and come back to https://www.domain.com/cats/wp-admin/post.php
    
    Just seen your last. I am assuming the code is okay insofar as when the site was created the code was copied from the codex, from memory. In any event, it has been used to submit about 1500 posts, so if there's something wrong then I'm baffled as to how it worked!!!
    I'll wait to see what you find.
    
    I can give you a site login if you want.
    #30773

    AITpro Admin
    Keymaster

    Or maybe maybe the htaccess RewriteRules will work for you since it sounds like there are other files involved in your CPT script on your website.  Try adding RewriteRules for cattery and county-cattery.

    Example:

    # WP REWRITE LOOP START
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    
    # Custom Post Types RewriteRules
    RewriteRule ^cattery/ - [L]
    RewriteRule ^county-cattery/ - [L]
    #30774

    Immerse
    Participant

    Just to clarify, I use the example code you provided above for both custom post types and taxonomies? Cattery is a cpt and county-cattery is a taxonomy. If correct it’ll take me a while as there are actually several..

     

    #30775

    AITpro Admin
    Keymaster

    hmm ok I did not know the posts were displaying correctly on the frontend of your site.  So maybe the problem is isolated to just the wp-admin htaccess file.  If that is the case then most likely you will need to add a whitelist rule in wp-admin Custom Code for these files:  edit.php, post.php and edit-tags.php.

    1. Add this wp-admin .htaccess bypass / skip rule below to the wp-admin Custom Code box – CUSTOM CODE WPADMIN PLUGIN FIXES:
    2. Click the Save wp-admin custom code button.
    3. Go to the Security Modes page and click the wp-admin Folder BulletProof Mode Activate button.

    Note: The skip rule must be [S=2] because it will be written to your wp-admin .htaccess file above skip / bypass rule [S=1]. This bypass / skip rule is safe to use because the wp-admin area is protected with WP Authentication security.

    # post.php skip/bypass rule
    RewriteCond %{REQUEST_URI} (post\.php|edit\.php|edit-tags\.php) [NC]
    RewriteRule . - [S=2]
    #30778

    Immerse
    Participant

    Well, I added the code you added for wp-admin and so far it hasn’t given a 404. However, it’s now taking about a minute to save a post. I can only tell you that this wasn’t the case this morning without bps, so I can only assume it’s directly related.

    I just don’t know what to do. I haven’t been able to leave my pc since installing after lunch. It’s now 11.00pm and and as of tomorrow I’m back to work full-time.

    #30779

    AITpro Admin
    Keymaster

    Hmm ok then it sounds like you multiple different problems going on.  I can’t think of anything in BPS Pro that would cause a post to take 1 minute or even a 10 second delay when saving a post.  Do BPS Pro troubleshooting steps #1 and #2 and test saving a post to completely eliminate that the Root and wp-admin htaccess files are causing the problem.  Then if the problem is still occurring do step #3 and test saving a post.  If the problem is still occurring after that then BPS Pro is not causing the slowness when saving a post.

    http://forum.ait-pro.com/forums/topic/read-me-first-pro/#bps-pro-general-troubleshooting

    In general, the slowness when saving a post problem you are describing would be caused by something like:  corrupt Browser Cache (clear your Browser cache), a caching plugin’s settings such as using Minification or some other setting, Cloudflare (especially Cloudflare Rocket), caching the wp-admin area (the wp-admin area should never be cached for any reason), ISP connectivity problems, host server issues/problems.  Another possible cause is using a Maintenance Mode/Coming Soon plugin. Some of them break overall website functionality.

    Also is the slowness when saving a post problem happening with ALL posts or just 1 post?  If it just happening with 1 post then the problem would be with the post contents itself.  ie extremely large or something WP does not like.

Viewing 15 posts - 1 through 15 (of 30 total)

You must be logged in to reply to this topic.