Hello,
Currently I’m using this snippet in my theme functions.php adding a select menu with an automatically generated list of shortcodes. When selected the selected shortcode tags will be automatically inserted into the text editor (more info about this snippet [[LINK href="http://bit.ly/iRmeYJ"]]here[[/LINK]])
/**
* Bouwstenen.
*/
function cor_media_buttons() {
global $shortcode_tags;
echo '<select id="bouwsteen"><option>Bouwstenen</option>';
foreach ($shortcode_tags as $key => $val) {
if (!in_array($key)) {
$shortcodes_list .= '<option value="[' . $key . '][/' . $key . ']">' . $key . '</option>';
}
}
echo $shortcodes_list;
echo '</select>';
}
add_action('media_buttons', 'cor_media_buttons', 11);
function cor_admin_head_js() {
echo '<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#bouwsteen").change(function() {
send_to_editor(jQuery("#bouwsteen :selected").val());
return false;
});
});
</script>';
}
add_action('admin_head', 'cor_admin_head_js');
Would it be possible to <strong>not</strong> show a generated list of shortcodes, but a list of generated post <strong>titles</strong> of custom post type 'lorem' <strong>and</strong> custom post type 'ipsum' instead?
AdamGold answers:
Should be something like:
/**
* Bouwstenen.
*/
function cor_media_buttons() {
global $shortcode_tags;
query_posts(array( 'post_type' => array('lorem', 'ipsum'), 'post_status' => 'publish', 'posts_per_page' => -1 ));
echo '<select id="bouwsteen"><option>Bouwstenen</option>';
if ( have_posts() ) {
while ( have_posts() ) { the_post();
$post_title = get_the_title();
echo '<option value="' . $post_title . '">' . $post_title . '</option>';
}
}
echo '</select>';
}
add_action('media_buttons', 'cor_media_buttons', 11);
function cor_admin_head_js() {
echo '<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#bouwsteen").change(function() {
send_to_editor(jQuery("#bouwsteen :selected").val());
return false;
});
});
</script>';
}
add_action('admin_head', 'cor_admin_head_js');
cor comments:
Hello Adam,
Nice we're finally doing business together!
@Christianto's solution did work, but since you were first, and you've added the extra post_status, which -in my newbyness- does matter, I've voted for you (:
Much appreciated!
Christianto answers:
/**
* Bouwstenen.
*/
function cor_media_buttons() {
global $shortcode_tags;
echo '<select id="bouwsteen"><option>Bouwstenen</option>';
$arg = array(
'post_type' => array( 'lorem', 'ipsum' ),
'posts_per_page' => -1
);
$loremipsum = new WP_Query($arg);
while($loremipsum->have_posts()): $loremipsum->the_post();
if (get_the_title( $post->ID )) {
$shortcodes_list .= '<option value="' .get_the_title( $post->ID ) . '">' . get_the_title( $post->ID ) . '</option>';
}
endwhile;
echo $shortcodes_list;
echo '</select>';
}
add_action('media_buttons', 'cor_media_buttons', 11);
function cor_admin_head_js() {
echo '<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#bouwsteen").change(function() {
send_to_editor(jQuery("#bouwsteen :selected").val());
return false;
});
});
</script>';
}
add_action('admin_head', 'cor_admin_head_js');
Sébastien | French WordpressDesigner answers:
<?php
/**
* Bouwstenen.
*/
function cor_media_buttons() {
global $post;
$args = array('post_type'=>array('lorem', 'ipsum'),'numberposts'=>-1);
echo '<select id="bouwsteen"><option>Bouwstenen</option>';
foreach(get_posts($args) as $key){
if (!in_array($key)) {
$shortcodes_list .= '<option value="[' . $key . '][/' . $key . ']">' . $key . '</option>';
}
}
echo $shortcodes_list;
echo '</select>';
}
add_action('media_buttons', 'cor_media_buttons', 11);
function cor_admin_head_js() {
echo '<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#bouwsteen").change(function() {
send_to_editor(jQuery("#bouwsteen :selected").val());
return false;
});
});
</script>';
}
add_action('admin_head', 'cor_admin_head_js');
?>