How To Integrate The WordPress Media Uploader Into Your Plugin
The following snippet will let you integrate the WordPress media uploader into your plugin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
// PHP, adds the appropriate scripts and styles in admin add_action('admin_print_scripts',array(&$this,'uploader_scripts')); add_action('admin_print_styles',array(&$this,'uploader_styles')); public function uploader_scripts(){ wp_enqueue_script('media-upload'); wp_enqueue_script('thinkbox'); } public function uploader_styles(){ wp_enqueue_style('thickbox'); } // The html for the button and hidden input for result (super simple) <input id="backdrop_upload_button" value="Upload/Select Image" type="button" class="button" /> <input type="hidden" name="the_image" id="the_image" value="<?= $settings['the_image'] ?>" /> // The jQuery to open the media uploader and get the response var $ = jQuery; // Handels image uploading $(document).ready(function(){ $("input#upload_button").click(function(){ tb_show('','media-upload.php?type=image&post_id=1&TB_iframe=true&flash=0&backdrop=true'); return false; }) window.send_to_editor=function(html){ var img = $('<div>'+html+'</div>').find('img').attr('src'); $('#the_image').val(img); tb_remove(); }; }); |
Snippet Source/Credit: FatFolderDesign
How To No-Follow WordPress Tag Cloud
The following snippet will let you have the no-follow WordPress tag cloud.
1 2 3 4 |
function add_nofollow_tag($sLink) { return str_replace('<a href=', '<a rel="nofollow" href=', $sLink); } add_filter('wp_tag_cloud', 'add_nofollow_tag'); |
Snippet Source/Credit: Snipplr
How To Limit WordPress the_content By Words
With the help of snippet below, you will be able to limit WordPress the_content by words.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//Add this function to your functions.php file function get_news($cat_slug, $limit){ $my_query = new WP_Query('category_name='.$cat_slug.'&posts_per_page='.$limit); $result = "<ul class='news-feed'>"; while ($my_query->have_posts()) : $my_query->the_post(); $result .= '<li class="news-item"><a href="' . get_permalink() . '" target="_blank">' . get_the_title() . '</a><p>'; $result .= apply_filters( 'the_content', wp_trim_words( get_the_content(), 15, '…' ) ) . ' <a href="' . get_permalink() . '" target="_blank" class="read_more"><strong>read more</strong> »</a>'; $result .= '</li>'; endwhile; $result .= '</ul>'; return $result; } //Usage: // get company news from the 'news' category with limit of 2 posts print get_news('news', 2); |
Snippet Source/Credit: StackExchange
How To List Author Comments On Author Page
Using the following snippet, you will be able to list all the author comments on authors page.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function wpse19316_author_comments( $length ) { $final_length = (int) $length; $author_comments = get_comments( array( 'ID' => $GLOBALS['authordata']->ID ) ); foreach ( $author_comments as $comment ) { $comment_length = sublen( $comment->comment_content ); $comment_excerpt = $comment->comment_content; if ( $comment_length > $final_length ) $comment_excerpt = substr( $comment->comment_content, $final_length ); echo $comment_excerpt.'<br />'; } } |
Snippet Source/Credit: StachExchange
First And Last Result Of WP_Query In WordPress
With the help of snippet below, one can get to know that when was the first and last time, user called the query.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$firstQuizSql = $wpdb->get_results("SELECT id, datetaken, person FROM aw_wpsqt_all_results WHERE person_name='$user_info->user_login' ORDER BY id"); $i = 1; foreach ($firstQuizSql as $firstQuizResult) { //first quiz result if ($i == 1) { echo $firstQuizResult->id . " "; } //last quiz result if ($i == count($firstQuizSql)) { echo $firstQuizResult->id . " "; } $i++; } |
Snippet Source/Credit: Snipplr