403 Error when Adding A New Code Snippet Code

Home Forums BulletProof Security Pro 403 Error when Adding A New Code Snippet Code

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #44953
    Ian
    Participant

    Hi,

    I am trying to add this code, to code snippets.

    When saving / trying to activate it, it comes up with a 403 error – ‘Could not create snippet. Request failed with status code 403’

    This is the code:

    /**
    * Alter the OpenGraph image for a single post
    */
    function alter_existing_opengraph_image( $image ) {
    global $gd_post;
    $logo = get_post_meta($gd_post->post_id,"geodir_logo",true);
    
    if( !empty( $logo ) ){
    $images_array = explode( '|', $logo );
    $image_url = ( !empty( $images_array[0] ))? $images_array[0]: '';
    $image_id = ( !empty( $images_array[1] ))? $images_array[1]: '';
    
    if ( !empty( $image_id ) ) {
    $image = wp_get_attachment_image_src( $image_id )[0];
    } elseif( !empty( $image_url ) ){
    $image = $image_url;
    }
    }
    return $image;
    }
    
    add_filter( 'rank_math/opengraph/facebook/image', 'alter_existing_opengraph_image', 20 );
    add_filter( 'rank_math/opengraph/twitter/image', 'alter_existing_opengraph_image', 20 );
    add_filter( 'wpseo_opengraph_image', 'alter_existing_opengraph_image' );

    Can you advise what I need to do please to allow this code to work?

    Thanks,

    #44954
    Ian
    Participant
    * Alter the OpenGraph image for a single post
    */
    function alter_existing_opengraph_image( $image ) {
    global $gd_post;
    $logo = get_post_meta($gd_post->post_id,"geodir_logo",true);
    
    if( !empty( $logo ) ){
    $images_array = explode( '|', $logo );
    $image_url = ( !empty( $images_array[0] ))? $images_array[0]: '';
    $image_id = ( !empty( $images_array[1] ))? $images_array[1]: '';
    
    if ( !empty( $image_id ) ) {
    $image = wp_get_attachment_image_src( $image_id )[0];
    } elseif( !empty( $image_url ) ){
    $image = $image_url;
    }
    }
    return $image;
    }
    
    add_filter( 'rank_math/opengraph/facebook/image', 'alter_existing_opengraph_image', 20 );
    add_filter( 'rank_math/opengraph/twitter/image', 'alter_existing_opengraph_image', 20 );
    add_filter( 'wpseo_opengraph_image', 'alter_existing_opengraph_image' );
    
    #44955
    Ian
    Participant

    I am trying to add the above code, to Code Snippets and am getting a 403 when trying to save / activate it

    Please advise what I need to do.

    Thank you

    #44957
    AITpro Admin
    Keymaster

    My guess would be ModSecurity or some other web host security measure is blocking this. The BPS Security Log logs all 403 errors whether BPS is blocking something or something else is blocking something. I think I have tested posted PHP code in Code Snippets, but I seem to recall that JS is normally what you would add using the default WP Widgets feature.  Also many years ago I seem to recall having to install an additional plugin in order to use PHP code vs JS code.

    #44959
    Ian
    Participant

    Hi,

    That code is supplied by the plugin (https://wpgeodirectory.com/) developer.

    Similar code was working before when we used Yoast SEO but because we have moved to rank math seo, they have supplied updated snippet code.

    #44960
    Ian
    Participant

    This was the working code when we used Yoast

    * Alter the OpenGraph image for a single post</div>
    function alter_existing_opengraph_image( $image ) {
    $logo = get_post_meta($gd_post->post_id,"geodir_logo",true);
    if( !empty( $logo ) ){
    $images_array = explode( '|', $logo );
    $image_url = ( !empty( $images_array[0] ))? $images_array[0]: '';
    $image_id = ( !empty( $images_array[1] ))? $images_array[1]: '';
    if ( !empty( $image_id ) ) {
    $image = wp_get_attachment_image_src( $image_id )[0];
    } elseif( !empty( $image_url ) ){
    $image = $image_url;
    }
    }<
    return $image;
    }<
    add_filter( 'wpseo_opengraph_image', 'alter_existing_opengraph_image' );
    #44961
    AITpro Admin
    Keymaster

    Post a BPS Security Log entry for what is being blocked, but can’t think of anything in BPS that would block this. So most likely you will need to contact your web host support and have them look at ModSecurity logs for what is being blocked.

    #44966
    Ian
    Participant
    [403 POST Request: April 24, 2025 12:52 pm]
    BPS Pro: 17.4
    WP: 6.8
    Event Code: WPADMIN-SBR
    Solution: https://forum.ait-pro.com/forums/topic/security-log-event-codes/
    REMOTE_ADDR: GDPR Compliance On
    Host Name: 45.133.173.23
    SERVER_PROTOCOL: HTTP/1.1
    HTTP_CLIENT_IP: GDPR Compliance On
    HTTP_FORWARDED: GDPR Compliance On
    HTTP_X_FORWARDED_FOR: GDPR Compliance On
    HTTP_X_CLUSTER_CLIENT_IP: GDPR Compliance On
    REQUEST_METHOD: POST
    HTTP_REFERER: https://walkingfootball.com/wp-admin/admin.php?page=edit-snippet&id=15
    REQUEST_URI: /wp-json/code-snippets/v1/snippets/15?
    QUERY_STRING:
    HTTP_USER_AGENT: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
    REQUEST BODY: BPS Security Log option set to: Do Not Log POST Request Body Data
    #44967
    AITpro Admin
    Keymaster

    To confirm or negate BPS as causing this problem deactivate wp-admin BulletProof Mode and let me know what happens when adding the code to Code Snippets.

    #44969
    Ian
    Participant

    Hi,

    Thanks, tried that and the issue still happened.

    So assume I need to kick this to the hosting company now?

    Cheers,

    Ian

     

    #44990
    AITpro Admin
    Keymaster

    Correct. My guess is that ModSecurity installed on your web host server is blocking adding code in Code Snippets.

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.