0

When my plugin is updated I am trying to write new values to the database. I am running through the post types and if it is Page Product or Post I want to place INDEX in to the DB. Else I want to place NO-INDEX into the DB. The INDEX part loops properly, or at least the best I can tell as the values are stored in the DB. But when it gets to the NO-INDEX, it processes the first post type, which is ATTACHMENTS, and then does not get or process the remaining post types. I have tried several variations on the code and cannot seem to get it to work correctly. What am I doing wrong here?

I am using add_option and register_setting

Here is the code:

function set_activation_value(){
    // Sets Field Defaults
    $option = get_option('ews_index_option_name');
    if (empty($option)) {
        $args = array (
            'public' => true
        ); 
        $post_types = get_post_types( $args, 'names' ); 
        $my_options = get_option('ews_index_option_name');
        $post_type_output = '';
        foreach ( $post_types as $post_type ) {
            if (($post_type == "page") || ($post_type == "product") || ($post_type == "post")) {
                $my_options = get_option('ews_index_option_name');
                $my_options["$post_type"] = 'index';
                update_option('ews_index_option_name', $my_options);
            }
            else if (($post_type != "page") && ($post_type != "product") && ($post_type != "post")) {
                $my_options = get_option('ews_index_option_name');
                $my_options["$post_type"] = 'no-index';
                update_option('ews_index_option_name', $my_options);
            }
            wp_reset_postdata();
            $post_type_output .= $post_type;
        }
        update_option('ews_index_my_types', $post_types);
        update_option('ews_index_option_var', $post_type_output);
    }
}
register_activation_hook( __FILE__, 'set_activation_value' );

When I run the update I get the following results: enter image description here

What I expect to get is: enter image description here

The database also shows the same data as what these pictures represent. I have also added other post types to check and on the else or if else it never gets past the attachments.

I have also tried the following code with no success:

foreach ( $post_types as $post_type ) {
    if (($post_type == "page") || ($post_type == "product") || ($post_type == "post")) {
        $my_options = get_option('ews_index_option_name');
        $my_options["$post_type"] = 'index';
        update_option('ews_index_option_name', $my_options);
    } else {
        $my_options = get_option('ews_index_option_name');
        $my_options["$post_type"] = 'no-index';
        update_option('ews_index_option_name', $my_options);
    }
}

Any help would be greatly appreciated, been at this for a while. I am not terribly good with loops and arrays, which may be apparent here. Thank you!

0

Let's see if this works. I can't really test this, but there are some things to update in your code:

function set_activation_value(){
    // Sets Field Defaults
    $option = get_option('ews_index_option_name');
    // get_option returns FALSE if it doesn't exist, so we check that here.
    if ( $option === FALSE ) {

        $args = array (
            'public' => true
        );

        $post_types = get_post_types( $args );
        // Since the option doesn't exist, we should add it, then update it in the loop.
        add_option('ews_index_option_name');
        // now we have an empty array.
        $my_options = [];
        $post_type_output = '';
        foreach ( $post_types as $post_type ) {
            // no need to wrap in parenthesis for each condition.
            if ( $post_type === 'page' || $post_type === 'product' || $post_type === 'post' ) {
                $my_options[ $post_type ] = 'index';
                update_option('ews_index_option_name', $my_options);
            // Skip the else if, since every other post type is no index.    
            } else {
                $my_options[ $post_type ] = 'no-index';
                update_option('ews_index_option_name', $my_options);
            }
            $post_type_output .= $post_type;
        }
        // These look to be holding the exact same data on one in an array and the other a string.
        update_option('ews_index_my_types', $post_types);
        update_option('ews_index_option_var', $post_type_output);
    }
}
register_activation_hook( __FILE__, 'set_activation_value' );

I cleaned it up a bit and added comments. Also, there is no reason to have wp_reset_postdata().

As said, I can't test this, but this might work better for you.

| improve this answer | |
  • I tried that and got the same results. Not sure about timing issues, or how to resolve it if that is an issue. But the custom post types from other plugins are pulled through as seen in the images attached above. But here it doesn't process the same even though the code is the same. This is being executed on plugin update if that sheds any light on it. I'll attach the code where it is being executed below. – Robert L Mar 26 at 17:49
  • // Checks the version number function myplugin_check_version() { if (EWS_METAS_VERSION !== get_option('ews_metas_version')) set_activation_value(); } add_action('plugins_loaded', 'myplugin_check_version'); function update_meta_function() { set_activation_value(); } add_action('upgrader_process_complete', 'update_meta_function'); – Robert L Mar 26 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.