3

I have a small form on my website, which I created using Jetpack plugin (it has built-in contact form creator).

I want to pull options for select input from my custom-post-type "artist" (using the titles of the posts). Is there a way to do it?

[contact-field label='Artist' type='select' required='1' options='i want my titles go here separated by commas plus "other" option'/]

The code for the field looks like this. I believe I need to do some php+jquery stuff in this page template, but I can't seem to get it.

brasofilo
  • 25,496
  • 15
  • 91
  • 179
websanya
  • 171
  • 1
  • 14
  • 1
    http://stackoverflow.com/questions/7695599/get-the-selected-index-value-of-select-tag-in-php – v0d1ch Dec 22 '12 at 15:52

1 Answers1

3

With some creativity, yes, it's possible :)

We create another Shortcode to create a "virtual" JetPack shortcode.

I tested this using the default post post_type.

add_shortcode( 'my-jet-form', 'so_14003883_jet_form' );

function so_14003883_jet_form( $atts, $content ) 
{
    // Query our post type, change accordingly
    $posts = get_posts( array(
        'post_type'    => 'post',
        'numberposts'  => -1,
        'post_status'  => 'publish'
    ) );

    // Build an array of post titles
    $titles = array();
    foreach( $posts as $post )
    {
        $titles[] = $post->post_title;
    }

    // Convert array into comma sepparated string
    $posts_select = implode( ',', $titles );

    // Make JetPack shortcode
    $return = do_shortcode( '[contact-form][contact-field label="Name" type="name" required="1"/][contact-field label="Artist" type="select" options="' . $posts_select . '"/][/contact-form]' );
    return $return;
}

Usage:

  • adjust the desired post type
  • adjust the do_shortcode part to suit your original shortcode
  • put the shortcode [my-jet-form] wherever you want
  • voilà
brasofilo
  • 25,496
  • 15
  • 91
  • 179