0

So I'm trying to create a custom plugin, which has a separate page on admin side where you can enable or disable comments on the whole page.

Here is the code that works but throws a php error:

Illegal string offset 'enode-checkbox'
wp-content/plugins/enode-plugin/enode.php:90

    function enode_settings_page()
{
    add_settings_section("section", "Comments", null, "enode");
    add_settings_field("enode-checkbox", "Disable Comments", "enode_checkbox_display", "enode", "section");
    register_setting("section", "enode-checkbox");
}

function enode_checkbox_display()
{
   ?>
        <!-- Here we are comparing stored value with 1. Stored value is 1 if user checks the checkbox otherwise empty string. -->
        <p>If you do not plan on having comments on your posts, products or anywhere on your site, it's best practice to completely disable comments to prevent spam bots overwhelming the site.</p>
        <input type="checkbox" name="enode-checkbox" value="1" <?php checked(1, get_option('enode-checkbox'), true); ?> />
   <?php
}

add_action("admin_init", "enode_settings_page");

function enode_page()
{
  ?>
      <div class="wrap">
         <h1>Enode Settings</h1>

         <form method="post" action="options.php">
            <?php
               settings_fields("section");

               do_settings_sections("enode");

               submit_button();
            ?>
         </form>
      </div>
   <?php
}


function my_admin_menu() {

$icon_url = plugins_url( 'enode-plugin/enode-setting.svg' );

add_menu_page(

__( 'Enode', 'enode-plugin-setting' ),

__( 'Enode', 'enode-plugin-setting' ),

'manage_options',

'sample-page',

'enode_page',

$icon_url

);

}



add_action( 'admin_menu', 'my_admin_menu' );


add_action('init', 'enodeDisableComments');
function enodeDisableComments() {
  $options = get_option( 'enode-checkbox' );

  // If is checked, activate function to disable comments
  if( $options['enode-checkbox'] == '1' ) {
    /* Remove Comments Entirely From Site */

    add_action('admin_init', function () {
        // Redirect any user trying to access comments page
        global $pagenow;

        if ($pagenow === 'edit-comments.php') {
            wp_redirect(admin_url());
            exit;
        }

        // Remove comments metabox from dashboard
        remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');

        // Disable support for comments and trackbacks in post types
        foreach (get_post_types() as $post_type) {
            if (post_type_supports($post_type, 'comments')) {
                remove_post_type_support($post_type, 'comments');
                remove_post_type_support($post_type, 'trackbacks');
            }
        }
    });

    // Close comments on the front-end
    add_filter('comments_open', '__return_false', 20, 2);
    add_filter('pings_open', '__return_false', 20, 2);

    // Hide existing comments
    add_filter('comments_array', '__return_empty_array', 10, 2);

    // Remove comments page in menu
    add_action('admin_menu', function () {
        remove_menu_page('edit-comments.php');
    });

    // Remove comments links from admin bar
    add_action('init', function () {
        if (is_admin_bar_showing()) {
            remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
        }
    });

    /* end of Remove Comments Entirely From Site */
  }
  else {
      //do nothing
  }
  return;
}

I'm not the first one to have this kind of an issue so I know the problem is, that when the checkbox is unchecked, it doesn't store anything in the database. So we need to check if there is something stored in the db.

So changing this line

  if( $options['enode-checkbox'] == '1' ) {

to this:

if ( isset( $options['enode-checkbox'] ) && $options['enode-checkbox'] == '1' ) {

should work but it doesn't.

3
  • hmmm try adding is_array( $options ) && to the condition... if options have never been saved, it will not be an array yet...
    – majick
    Commented Feb 1, 2021 at 23:40
  • No error but still doesn't work
    – Gert
    Commented Feb 1, 2021 at 23:44
  • What is not working, the option saving? Can you not just output the $options value to check that? Also you need to add a different priority to the second init action since it's nested inside the first, or it probably won't run.
    – majick
    Commented Feb 2, 2021 at 0:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.