0

I hope you can help! I have created a relatively simple admin settings page, that has 3 fields:

page IDs, post IDs, Text Box

What I want to do, is display the HTML content of Text Box at the top of the content of any page or post that isn't in the page IDs or post IDs fields. my code allows me to get_option('field UID'), so I figured I would define the post ID as a variable using get_option, and then stick that into an is_single() check to define whether to echo the Text Box or not.

That works fine with a single ID, but if I put multiple IDs into the post IDs field, separated by a comma (e.g. 4519, 4133, 201) then it stops working, and instead shows the Text Box content on all posts again... What am I doing wrong?!

Here's my code: (the add_action is right at the bottom).

<?php
            class Codeable_Fields_Plugin {
                public function __construct() {
                    // Hook into the admin menu
                    add_action( 'admin_menu', array( $this, 'create_plugin_settings_page' ) );
                    // Add Settings and Fields
                    add_action( 'admin_init', array( $this, 'setup_sections' ) );
                    add_action( 'admin_init', array( $this, 'setup_fields' ) );
                }
                public function create_plugin_settings_page() {
                    // Add the menu item and page
                    $page_title = 'Custom HTML';
                    $menu_title = 'Custom HTML';
                    $capability = 'manage_options';
                    $slug = 'codeable_fields';
                    $callback = array( $this, 'plugin_settings_page_content' );
                    $position = 100;
                    add_menu_page( $page_title, $menu_title, $capability, $slug, $callback, $icon, $position );
                }
                public function plugin_settings_page_content() {?>
                    <div class="wrap">
                        <?php
                        if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] ){
                              $this->admin_notice();
                        } ?>
                        <form method="POST" action="options.php">
                            <?php
                                settings_fields( 'codeable_fields' );
                                do_settings_sections( 'codeable_fields' );
                                submit_button();
                            ?>
                        </form>
                    </div> <?php
                }

                public function admin_notice() { ?>
                    <div class="notice notice-success is-dismissible">
                        <p>Your settings have been updated!</p>
                    </div><?php
                }
                public function setup_sections() {
                    add_settings_section( 'our_first_section', 'Custom HTML', array( $this, 'section_callback' ), 'codeable_fields' );
                }
                public function section_callback( $arguments ) {
                    switch( $arguments['id'] ){
                        case 'our_first_section':
                            echo '';
                            break;
                    }
                }
                public function setup_fields() {
                    $fields = array(
                        array(
                            'uid' => 'codeable_pages_field',
                            'label' => 'Page IDs to Exclude',
                            'section' => 'our_first_section',
                            'type' => 'text',
                            'supplimental' => 'Seperate by comma',
                        ),
                         array(
                            'uid' => 'codeable_posts_field',
                            'label' => 'Post IDs to Exclude',
                            'section' => 'our_first_section',
                            'type' => 'text',
                            'supplimental' => 'Seperate by comma',
                        ),

                        array(
                            'uid' => 'codeable_textarea',
                            'label' => 'Enter Text here',
                            'section' => 'our_first_section',
                            'type' => 'textarea',
                        )

                    );
                    foreach( $fields as $field ){
                        add_settings_field( $field['uid'], $field['label'], array( $this, 'field_callback' ), 'codeable_fields', $field['section'], $field );
                        register_setting( 'codeable_fields', $field['uid'] );
                    }
                }
                public function field_callback( $arguments ) {
                    $value = get_option( $arguments['uid'] );
                    if( ! $value ) {
                        $value = $arguments['default'];
                    }
                    switch( $arguments['type'] ){
                        case 'text':
                        case 'password':
                        case 'number':
                            printf( '<input name="%1$s" id="%1$s" type="%2$s" placeholder="%3$s" value="%4$s" />', $arguments['uid'], $arguments['type'], $arguments['placeholder'], $value );
                            break;
                        case 'textarea':
                            printf( '<textarea name="%1$s" id="%1$s" placeholder="%2$s" rows="5" cols="50">%3$s</textarea>', $arguments['uid'], $arguments['placeholder'], $value );
                            break;
                        case 'select':
                        case 'multiselect':
                            if( ! empty ( $arguments['options'] ) && is_array( $arguments['options'] ) ){
                                $attributes = '';
                                $options_markup = '';
                                foreach( $arguments['options'] as $key => $label ){
                                    $options_markup .= sprintf( '<option value="%s" %s>%s</option>', $key, selected( $value[ array_search( $key, $value, true ) ], $key, false ), $label );
                                }
                                if( $arguments['type'] === 'multiselect' ){
                                    $attributes = ' multiple="multiple" ';
                                }
                                printf( '<select name="%1$s[]" id="%1$s" %2$s>%3$s</select>', $arguments['uid'], $attributes, $options_markup );
                            }
                            break;
                        case 'radio':
                        case 'checkbox':
                            if( ! empty ( $arguments['options'] ) && is_array( $arguments['options'] ) ){
                                $options_markup = '';
                                $iterator = 0;
                                foreach( $arguments['options'] as $key => $label ){
                                    $iterator++;
                                    $options_markup .= sprintf( '<label for="%1$s_%6$s"><input id="%1$s_%6$s" name="%1$s[]" type="%2$s" value="%3$s" %4$s /> %5$s</label><br/>', $arguments['uid'], $arguments['type'], $key, checked( $value[ array_search( $key, $value, true ) ], $key, false ), $label, $iterator );
                                }
                                printf( '<fieldset>%s</fieldset>', $options_markup );
                            }
                            break;
                    }
                    if( $helper = $arguments['helper'] ){
                        printf( '<span class="helper"> %s</span>', $helper );
                    }
                    if( $supplimental = $arguments['supplimental'] ){
                        printf( '<p class="description">%s</p>', $supplimental );
                    }
                }
            }
            new Codeable_Fields_Plugin();


            add_action ('the_content', 'add_to_header');
            function add_to_header(){

                $postids = get_option('codeable_posts_field');

                if (is_single(array( $postids ))) {
                    echo '';
                }
                elseif (is_single()) {
                   echo get_option('codeable_textarea');
                }  

            } 

It also seems to strip out any HTML tags I add into the Text Box, I want to have them read as HTML and display accordingly on the front-end. (e.g. if I add a text it would display as text on the frontend, wrapped in a div.

0
$postids = get_option('codeable_posts_field');

This will give you a string of comma separated post ids (according to your code, and if the user follows the instructions).

array( $postids )

will turn it into an array with that string as the only element. That won't work with is_single(), obviously, because is_single doesn't do any further splitting.

Here's how to turn that string into a proper array using preg_split. Replace

$postids = get_option('codeable_posts_field');

with

$postids = preg_split("/\s*,\s*/", get_option('codeable_posts_field'));

preg_split uses a regular expression to split a string into an array. I've added \s* before and after the comma so that it will work for 123,456 and also for 123, 456 or 123 , 456.

With $postids being an array, your check should work just fine. If it doesn't, try

var_export($postids);

to see what $postids really contains.

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.