• Resolved BDR Bros.

    (@bdrbros)


    Hello,

    My snippets use the “wp” hook. When I go to the “all reviews” tab, the plugin triggers the ‘wp’ hook multiple times, which causes a fatal error: “(PHP Fatal error: Cannot redeclare redirect_author_page_to_home() (previously declared in…)”. I have a lot of snippets and I would prefer not to change hook to something other than “wp”.

    Best,
    Alex

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Gemini Labs

    (@geminilabs)

    What is the rest of the error message?

    FYI: Site Reviews does not use the wp hook. Also, you should add a is_admin() check to your code if it is only meant to run on the frontend.

    • This reply was modified 9 months, 1 week ago by Gemini Labs.
    Thread Starter BDR Bros.

    (@bdrbros)

    [16-Feb-2024 10:33:24 UTC] PHP Fatal error: Cannot redeclare redirect_author_page_to_home() (previously declared in /home/domain.pl/public_html/wp-content/plugins/wpcodebox2/src/Runner/QueryRunner.php(126) : eval()'d code:10) in /home/domain.pl/public_html/wp-content/plugins/wpcodebox2/src/Runner/QueryRunner.php(126) : eval()'d code on line 9
    

    I spoke to the developer of WPCodeBox and he is unable to do anything about this fact. I can only anonymize the function or change the hooks.

    Thread Starter BDR Bros.

    (@bdrbros)

    Exaple:

    add_action( 'template_redirect', 'my_redirect_author_page_to_home' );
    
    function redirect_author_page_to_home() {

    to:

    add_action( 'template_redirect', function() {

    And then it works properly.

    • This reply was modified 9 months, 1 week ago by BDR Bros..
    Plugin Author Gemini Labs

    (@geminilabs)

    Firstly: I doubt this is being caused by Site Reviews. Does the error include a stack trace that shows files from Site Reviews are involved?

    Secondly: add a function_exists check to your snippet:

    if (!function_exists('redirect_author_page_to_home')) {
        function redirect_author_page_to_home() {
            // ...
        }
    }
    add_action('template_redirect', 'redirect_author_page_to_home');

    Thirdly: redirect_author_page_to_home is a custom function but the error shows that it is being defined more than once. That means you are triggering the snippet that is creating the function more than once. Adding a function_exists check is more of a bandaid fix. For a proper solution, you need to figure out why your snippet is being triggered more than once. That might involve using a different hook, or adding an is_admin check before creating the function (if the snippet should only run on the admin side/frontend).

    Thread Starter BDR Bros.

    (@bdrbros)

    Out of several thousand subpages, the critical error occurs only on the “/wp-admin/edit.php?post_type=site-review” subpage, so I concluded that the conflict is on the WPCodeBox <-> Site Reviews line.

    The name of the function does not matter, it can be completely random and it is still performed several times on this subpage. If there is no other solution, they will modify their snippets according to your recommendation. Thank you for your tips.

    Plugin Author Gemini Labs

    (@geminilabs)

    In that case, please provide the entire snippet so I can advise you better.

    Thread Starter BDR Bros.

    (@bdrbros)

    There is no need. I now know how to fix this problem myself.

    I thought it might be a problem in the plugin code, if not I will fix my snippets to make them compatible.

    Thank you again for your help ??

    Thread Starter BDR Bros.

    (@bdrbros)

    Can you do such a test yourself? This won’t seem correct.

    https://screenrec.com/share/MdJc3k6DuA

    https://screenrec.com/share/2ZO4TfVp1i

    Plugin Author Gemini Labs

    (@geminilabs)

    Site Reviews uses the load-edit.php and views_edit-site-review hooks to override the WP_Posts_List_Table class with a custom one.

    Before WordPress v6.1.0 introduced the wp_list_table_class_name hook, there was no way to override the WP_Posts_List_Table class before it was initialized. In order to provide compatibility with WordPress < 6.1, Site Reviews does the following:

    1. Initializes a custom ListTable class after the WP_Posts_List_Table class has already been initialized and the main query setup (this is where the wp hook is first triggered since it is run in the wp_edit_posts_query function).
    2. Overrides the global $wp_list_table variable with the custom ListTable class.

    Since the custom ListTable class extends the WP_Posts_List_Table, it means the main query is setup a second time (though the main query has already been cached at this point so there is no performance hit). This is why wp is triggered twice on the All Reviews page.

    Site Reviews v7.0.0 drops support for WordPress < 6.1, so you have two options:

    1. Wait for Site Reviews v7.0.0 to be released at end of the month(ish) and continue to use the high-level wp hook and perform some checks to ensure your code is only run once (i.e. If your custom function does not exist, run your code. If it does, skip the code).
    2. Use a more appropriate hook for your code snippet (i.e. unless your code needs to be run on every single page load of your several thousand pages, choose a more focused hook that is only run when needed and only on the pages needed).

    See also: https://developer.www.ads-software.com/apis/hooks/action-reference/

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Conflict with “wp” hook’ is closed to new replies.