0

I have created a plugin that allows entry of a custom post type. Within that post type I have a select metabox for name. I also have an options page that has a textarea box that has a list of names. I have been unsuccessful at connecting the 2 together. The idea is to have the list from the textarea show as the options for the select box in the plugin. any direction on how to accomplish this would be greatly appreciated.

Code in options page for Textarea

<th scope="row">Name</th>
    <td><textarea id="name" name="name" rows="5" cols="36"><?php echo esc_attr( get_option('name') ); ?></textarea></td>

Code in Plugin for select box

$options = get_option( 'name' );

$names = explode( "", $options['name'] );
?>

<select name="Name">
<?php foreach ( $names as $name ) {
printf(
    '<option value="%s">%s</option>',
    $name,
    $name
);
} ?>

Image of the admin area not the var_dump shows Michelle but the name in the select box is John.

0

You can use get_option to get your option values. Below is an example

<?php
$selected_name = get_post_meta( get_the_ID(), 'your_meta_key', true );
$options = get_option( 'your_options' );
// If your names are separeated with comma
$names = explode( PHP_EOL, $options);
?>

<select name="your-select-name">
<?php foreach ( $names as $name ) {
    printf(
        '<option value="%s" %s>%s</option>',
        $name,
        selected( $name, $selected_name, false ),
        $name
    );
} ?>
</select>
  • @JohnLucey What you get if you var_dump( get_option( 'name' )? – ville6000 Nov 12 '15 at 13:47
  • How do I post Code into this forum? I would love to show it here so you could look at it but I have had no success with that either. Ever feel like the world is working against ya lol? – John Lucey Nov 12 '15 at 13:49
  • You can try gist.github.com or pastebin – ville6000 Nov 12 '15 at 13:53
  • Oh no I meant how do I past the code in here like you did above with the grey box and formatting and all. – John Lucey Nov 12 '15 at 13:57
  • Oh, click curly braces from the editor. Code must be indented four spaces. – ville6000 Nov 12 '15 at 14:02

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.