0

In my plugin, I've created a textfield (ID: main_menu) that allows the users to list the admin menu items by their menu slug (ex: index.php = Dashboard, plugins.php = Plugins, etc.):

It's important that the user enters each menu item in a new line so this can convert it to an array. Right now, when the user enters in their input on each line, the value for that textfield is treated as one item when I echo the get_option() for that field.

How do I convert each new line input that's entered into an array loop of multiple values?

0

To do so, you will want to use the explode() function to break up each new line (\n) into another array value. Also it is important to replace the invisible Carriage Return which is also used as a new line character by Mac OS:

# Get the plugin options
$settings = get_option( 'myplugin_menu' );
if ( isset( $settings['main_menu'] ) ) {
    # Convert each new line in the textarea as an array item
    $menu_slug = explode( "\n", str_replace( "\r", "", $settings['main_menu'] ) );
    foreach ( $menu_slug as $key => $value ) {
        # Remove each menu item
        remove_menu_page( $value );
    }
}

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.