Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //-------------------------------------------------------------------
- // using dynamic sidebars
- //-------------------------------------------------------------------
- if (function_exists(‘register_sidebar’))
- register_sidebar(array(‘name’ => ‘sidebar1?,
- ’before_widget’ => ‘<li id=”%1$s” class=”widget %2$s”>’,
- ’after_widget’ => ‘</li>’,
- ’before_title’ => ‘<h2 class=”widgettitle”>’,
- ’after_title’ => ‘</h2>’,
- ));
- //-----------------------------------------------------------
- // altering widgets output dynamically
- //-----------------------------------------------------------
- add_filter(‘dynamic_sidebar_params’,
- ‘reclamation_sidebar_params’);
- //------------------------------------------------
- function reclamation_sidebar_params($params){
- //------------------------------------------------
- $params[0]['before_widget'] = '<div class=”block”><div class=”block_title”>';
- $params[0]['before_title'] =
- '<h3><img src=”' .
- get_bloginfo(‘stylesheet_directory’) .
- '/sidebartitles.php?letters=' .
- @$params[0]['widget_name'] . '” />';
- $params[0]['after_title'] = '</h3></div>';
- $params[0]['after_widget'] =
- '</div><div class=”block_bottom”>'
- return $params;
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // tweaking output of widgets in the sidebar
- //-------------------------------------------------------------------
- function my_edit_widget_func($params) {
- $params[0]['before_title'] = '<h3 class="' . $params[0]['widget_name'] . '">' ;
- return $params;
- }
- add_filter('dynamic_sidebar_params',
- 'my_edit_widget_func');
- ?>
- <?php
- //-------------------------------------------------------------------
- // using ( pre_get_posts ) action hook
- //-------------------------------------------------------------------
- //----------------------------------------------
- // output posts with custom type on home page
- //----------------------------------------------
- function some_name($query) {
- if ( $query->is_main_query() && $query->is_home() ) {
- $query->set( 'post_type', array( 'home_portfolio' ) );
- }
- }
- add_action( 'pre_get_posts', 'some_name');
- //----------------------------------------------
- // output posts with custom type on front page
- //----------------------------------------------
- function wpse83754_filter_pre_get_posts( $query ) {
- if ( $query->is_main_query() && is_front_page() ) {
- $query->set( 'post_type', array( 'home_portfolio' ) );
- }
- }
- add_action( 'pre_get_posts', 'wpse83754_filter_pre_get_posts' );
- //----------------------------------------------
- // altering output on single page
- //----------------------------------------------
- function wpse83754_filter_pre_get_posts( $query ) {
- if ( $query->is_main_query() && is_page( $id ) ) {
- $query->set( 'post_type', array( 'home_portfolio' ) );
- }
- }
- add_action( 'pre_get_posts', 'wpse83754_filter_pre_get_posts' );
- //----------------------------------------------
- // exclude certain category from output
- //----------------------------------------------
- add_action( 'pre_get_posts', 'foo_modify_query_exclude_category' );
- function foo_modify_query_exclude_category( $query ) {
- if ( ! is_admin() && $query->is_main_query() && ! $query->get( 'cat' ) )
- $query->set( 'cat', '-5' );
- }
- //----------------------------------------------
- // check if we are in the loop
- //----------------------------------------------
- in_the_loop(); ?>
- <?php
- //-------------------------------------------------------------------
- // altering post content before output (in main cycle)
- //-------------------------------------------------------------------
- add_filter ('the_excerpt', array ($this, 'insert_biography_box'));
- add_filter ('the_content', array ($this, 'insert_biography_box'));
- //------------------------------------------
- function insert_biography_box ($content) {
- //------------------------------------------
- error_log ('insert_biography_box: current filter=' . current_filter ());
- if (!is_main_query ()) {
- error_log ('Not main query, baling');
- return $content;
- }
- $biography = 'some-magic-function-return-value';
- return $content . $biography;
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // registering multiple dynamic sidebars
- //-------------------------------------------------------------------
- if(function_exists('register_sidebars')){
- register_sidebars(2, array(
- 'name'=>'sidebar%d',
- 'id' =>'sidebar',
- 'before_widget'=>'<li class="box">',
- 'after_widget'=>'</li><!--box--><!--aside-->',
- 'before_title'=>'<h2>',
- 'after_title'=>'</h2>',
- ));
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // altering widget params before output in sidebar
- //-------------------------------------------------------------------
- add_filter('dynamic_sidebar_params','io_blogroll_class');
- //---------------------------------------
- function io_blogroll_class($params){
- //---------------------------------------
- $params[0]['before_widget'] = '<li class="'.$params[0]['widget_name'].' box">';
- $params[0]['after_widget'] = '</li><!--'.$params[0]['widget_name'].'-->';
- if ($params[0]['widget_name'] == "Links"){
- $params[0]['before_widget'] = '<li class="'.$params[0]['widget_name'].'">';
- $params[0]['after_widget'] = '</li><!--'.$params[0]['widget_name'].'-->';
- }
- return $params;
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // altering post content before output (in main cycle)
- //-------------------------------------------------------------------
- add_filter ('the_excerpt', array ($this, 'insert_biography_box'));
- add_filter ('the_content', array ($this, 'insert_biography_box'));
- function insert_biography_box ($content) {
- if (!in_the_loop () || !is_main_query ()) {
- return $content;
- }
- $biography = 'some-magic-function-return-value';
- return $content . $biography;
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // registering custom Widget
- //-------------------------------------------------------------------
- add_action( 'widgets_init', 'register_my_widget' ); // Загрузка виджета
- function register_my_widget() {} // Функция регистрации виджета
- class My_Widget extends WP_Widget () {} //Класс виджета (как пример)
- function My_Widget() {} // Установки виджета
- function widget() {} // Вывод виджета
- function update() {} // Обновление виджета
- function form() {} // Форма для опций виджета
- //-------------------------------------------------------------------
- // different output functions
- //-------------------------------------------------------------------
- ?>
- <?php wp_list_pages('title_li=<h2>Pages</h2>'); ?>
- <li id="categories"><?php _e('Categories:'); ?>
- <ul>
- <?php wp_list_cats(); ?>
- </ul>
- </li>
- <div class="navigation">
- <div class="alignleft">
- <?php next_posts_link('« Previous Entries') ?>
- </div>
- <div class="alignright">
- <?php previous_posts_link('Next Entries »') ?>
- </div>
- </div>
- <li id="archives"><?php _e('Archives:'); ?>
- <ul>
- <?php wp_get_archives('type=monthly'); ?>
- </ul>
- <?php wp_get_archives('type=postbypost&limit=15'); ?>
- <?php get_links_list(); ?>
- <ul><?php get_links(2, '<li>', '</li>', '', TRUE, 'url', FALSE); ?></ul>
- <h2>Link list of authors:</h2>
- <ul>
- <?php
- $order = 'user_nicename';
- $user_ids = $wpdb->get_col("SELECT ID FROM $wpdb->users ORDER BY $order"); // query users
- foreach($user_ids as $user_id) : // start authors' profile "loop"
- $user = get_userdata($user_id);
- ?>
- <li>
- <?php echo '<a href="' . $user->user_url . '">' . $user->display_name . '</a>'; ?>
- <br /></li>
- <?php
- endforeach; // end of authors' profile 'loop'
- ?>
- </ul>
- <?php get_sidebar(); ?>
- <?php include ('sidebar-cat.php'); ?>
- <?php include ('sidebar-archive.php'); ?>
- <?php
- //-------------------------------------------------------------------
- // hide certain widgets from certain sidebar on single page
- //-------------------------------------------------------------------
- add_filter( 'sidebars_widgets', 'wpse17681_sidebars_widgets' );
- function wpse17681_sidebars_widgets( $sidebars_widgets )
- {
- if ( is_page() /* Or whatever */ ) {
- foreach ( $sidebars_widgets as $sidebar_id => &$widgets ) {
- if ( 'my_sidebar' != $sidebar_id ) {
- continue;
- }
- foreach ( $widgets as $idx => $widget_id ) {
- if ( 0 === strncmp( $widget_id, 'links-', 6 ) ) {
- unset( $widgets[$idx] );
- }
- }
- }
- }
- return $sidebars_widgets;
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // admin bar is present on screen
- //-------------------------------------------------------------------
- if ( is_admin_bar_showing() ) {
- // do something
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // altering output of function checking if post is open for comments
- //-------------------------------------------------------------------
- add_filter( 'comments_open', 'my_comments_open', 10, 2 );
- //-----------------------------------------------
- function my_comments_open( $open, $post_id ) {
- //-----------------------------------------------
- $post = get_post( $post_id );
- if ( 'page' == $post->post_type )
- $open = false;
- return $open;
- }
- //-------------------------------------------------------------------
- // enqueue custom script
- //-------------------------------------------------------------------
- add_action( 'wp_print_scripts', 'myplugin_scripts' );
- //--------------------------------
- function myplugin_scripts(){
- //--------------------------------
- if ( is_single() && comments_open() ) {
- // myplugin_script must have been previously registered via wp_register_script()
- wp_enqueue_script( 'myplugin_script' );
- }
- if ( is_active_widget( false, false, $this->id_base, true ) ) {
- wp_enqueue_script( 'jquery' );
- }
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // registering custom widget
- //-------------------------------------------------------------------
- class MyNewWidget extends WP_Widget {
- //------------------------------
- function MyNewWidget() {
- parent::__construct( false, 'My New Widget Title' );
- }
- //------------------------------
- function widget( $args, $instance ) {
- // Widget output
- }
- //------------------------------
- function update( $new_instance, $old_instance ) {
- // Save widget options
- }
- //------------------------------
- function form( $instance ) {
- // Output admin widget options form
- }
- }
- //------------------------------
- function myplugin_register_widgets() {
- register_widget( 'MyNewWidget' );
- }
- //------------------------------
- add_action( 'widgets_init', 'myplugin_register_widgets' );
- //-------------------------------------------------------------------
- // output standart widget
- //-------------------------------------------------------------------
- the_widget( 'WP_Widget_Archives', $instance, $args ); ?>
- <?php
- //-------------------------------------------------------------------
- // Call a specific sidebar widget
- //-------------------------------------------------------------------
- // For your functions.php
- /**
- * Filter callback
- *
- * @param array $sidebars_widgets
- * @return mixed bool/object $sidebars_widgets The widget or FALSE if not found
- */
- //-----------------------------------------------------
- function filter_sidebars_widgets( $sidebars_widgets )
- //-----------------------------------------------------
- {
- $wanted = 'recent-posts-2';
- //---------------------------------------------------------
- // Prevent intercepting another call - on demand filter(!)
- //---------------------------------------------------------
- remove_filter( current_filter(), __FUNCTION__ );
- if ( is_array( $sidebars_widgets ) )
- {
- //---------------------------------------------
- // array_search() returns FALSE
- // in case the widget isn't present
- //---------------------------------------------
- $index = array_search( $wanted, $sidebars_widgets, FALSE );
- $sidebars_widgets = $sidebars_widgets[ $index ];
- }
- else
- {
- //------------------------------------
- // we add a manual FALSE
- // in case the widget isn't present
- //------------------------------------
- $sidebars_widgets = $wanted === $sidebars_widgets ? $sidebars_widgets : FALSE;
- }
- return $sidebars_widgets;
- }
- //-----------------------------------------------
- // In your template: First add the filter...
- //-----------------------------------------------
- add_filter( 'sidebars_widgets', 'filter_sidebars_widgets' );
- //-----------------------------------------------
- // ...then call the function.
- //-----------------------------------------------
- $widget = wp_get_sidebars_widgets();
- // Now do something with $widget :)
- // As we removed the filter inside the callback, any other call
- // to wp_get_sidebars_widgets(); will behave like normal.
- //-------------------------------------------------------------------
- /*
- * wrapper function for showing sidebars, so that we can customise widget output
- * @param int|string $index Optional, default is 1. Name or ID of dynamic sidebar.
- * @return bool True, if widget sidebar was found and called. False if not found or not called.
- */
- //------------------------------------------------------------------
- function theme_dynamic_sidebar($index = 1) {
- // capture output from the widgets
- ob_start();
- $result = dynamic_sidebar($index);
- $out = ob_get_clean();
- //---------------------------------------------------
- // replace SendBlaster OptIn widget
- // Location custom field with drop-down list
- //---------------------------------------------------
- $list = <<<HTML
- Location <select size="1" name="$1" id="$2" >
- <option value=""></option>
- <option value="ACT">ACT</option>
- <option value="SA">SA</option>
- <option value="NT">NT</option>
- <option value="QLD">QLD</option>
- <option value="NSW">NSW</option>
- <option value="VIC">VIC</option>
- <option value="TAS">TAS</option>
- <option value="WA">WA</option>
- <option value="NZ">New Zealand</option>
- <option value="US">United States</option>
- <option value="Other">Other</option>
- </select>
- HTML;
- $out = preg_replace(
- '|Location <input type="text" name="([^"]+)" id="([^"]+)"(?:[^>])*>|', $list, $out);
- //----------------------------------------
- // finally, output whatever we have left
- //----------------------------------------
- echo $out;
- return $result;
- }
- //------------------------------------------
- theme_dynamic_sidebar('left_sidebar');
- ?>
- <?php
- //-------------------------------------------------------------------
- // altering comments before output
- //-------------------------------------------------------------------
- function stefan_wrap_comment_text($content) {
- return "<div class=\"comment-text\">". $content ."</div>";
- }
- //----------------------
- add_filter('comment_text', 'stefan_wrap_comment_text');
- add_filter('comment_text', 'stefan_wrap_comment_text', 1000);
- ?>
- <?php
- //-------------------------------------------------------------------
- // altering posts before output on search page
- //-------------------------------------------------------------------
- function my_the_posts($posts, $query = false) {
- if( is_search() ){
- // do your thing here
- }
- return $posts;
- }
- add_filter( 'the_posts', 'my_the_posts' );
- ?>
- <?php
- //-------------------------------------------------------------------
- // conditional output of scripts and styles (any page)
- //-------------------------------------------------------------------
- // the_posts gets triggered before wp_head
- //-------------------------------------------------------------------
- add_filter('the_posts', 'conditionally_add_scripts_and_styles');
- //-------------------------------------------------------
- function conditionally_add_scripts_and_styles($posts){
- //-------------------------------------------------------
- if (empty($posts)) return $posts;
- //--------------------------------------------
- // use this flag to see
- // if styles and scripts need to be enqueued
- //--------------------------------------------
- $shortcode_found = false;
- foreach ($posts as $post) {
- if (stripos($post->post_content, '[code]') !== false) {
- $shortcode_found = true; // bingo!
- break;
- }
- }
- if ($shortcode_found) {
- wp_enqueue_style('my-style', '/style.css');
- wp_enqueue_script('my-script', '/script.js');
- }
- return $posts;
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // add custom tab in media editor
- //-------------------------------------------------------------------
- add_filter('media_upload_tabs',
- 'my_media_upload_tabs_filter');
- //----------------------------------------------
- function my_media_upload_tabs_filter($tabs) {
- //----------------------------------------------
- unset($tabs["type_url"]);
- unset($tabs['library']);
- $newtab = array('ell_insert_gmap_tab' => __('Google Map','insertgmap'));
- return array_merge($tabs,$newtab);
- }
- add_action('media_upload_ell_insert_gmap_tab',
- 'media_upload_ell_gmap_tab');
- //----------------------------------------------
- function media_upload_ell_gmap_tab() {
- //----------------------------------------------
- return wp_iframe(
- 'media_upload_ell_gmap_form'
- , $errors );
- }
- //----------------------------------------------
- function media_upload_ell_gmap_form() {
- //----------------------------------------------
- echo media_upload_header();
- ?>
- <h2>HTML Form</h2>
- <?php
- }
- //-------------------------------------------------------------------
- // add custom media tab
- //-------------------------------------------------------------------
- // add the tab
- //--------------------------------------
- add_filter('media_upload_tabs',
- 'my_upload_tab');
- //------------------------------
- function my_upload_tab($tabs) {
- //------------------------------
- $tabs['mytabname'] = "My Tab Name";
- return $tabs;
- }
- // call the new tab with wp_iframe
- //--------------------------------------
- add_action('media_upload_mytabname',
- 'add_my_new_form');
- //-----------------------------
- function add_my_new_form() {
- //-----------------------------
- wp_iframe( 'my_new_form' );
- }
- // the tab content
- //--------------------------------------
- function my_new_form() {
- echo '<p>Example HTML content goes here.</p>';
- }
- ?>
- //-------------------------------------------------------------------
- // redirect user on previous page after login
- //-------------------------------------------------------------------
- <a href="<?php echo wp_login_url( get_permalink() ); ?>" title="Login">Login</a>
- <?php
- //-------------------------------------------------------------------
- // create custom rewrite rules
- //-------------------------------------------------------------------
- add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' );
- add_filter( 'query_vars','my_insert_query_vars' );
- add_action( 'wp_loaded','my_flush_rules' );
- // flush_rules() if our rules are not yet included
- //-----------------------------------------------------
- function my_flush_rules(){
- //-----------------------------------------------------
- $rules = get_option( 'rewrite_rules' );
- if ( ! isset( $rules['(project)/(\d*)$'] ) ) {
- global $wp_rewrite;
- $wp_rewrite->flush_rules();
- }
- }
- // Adding a new rule
- //-----------------------------------------------------
- function my_insert_rewrite_rules( $rules )
- //-----------------------------------------------------
- {
- $newrules = array();
- $newrules['(project)/(\d*)$'] = 'index.php?pagename=$matches[1]&id=$matches[2]';
- return $newrules + $rules;
- }
- // Adding the id var so that WP recognizes it
- //-----------------------------------------------------
- function my_insert_query_vars( $vars )
- //-----------------------------------------------------
- {
- array_push($vars, 'id');
- return $vars;
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // add custom rewrite rules via rewrite tag
- //-------------------------------------------------------------------
- function keywords_create_rewrite_rules( $rewrite ) {
- global $wp_rewrite;
- // add rewrite tokens
- $keytag_token = '%tag%';
- $wp_rewrite->add_rewrite_tag( $keytag_token, '(.+)', 'tag=' );
- $keywords_structure = $wp_rewrite->root . "tag/$keytag_token";
- $keywords_rewrite = $wp_rewrite->generate_rewrite_rules( $keywords_structure );
- return ( $rewrite + $keywords_rewrite );
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // add rewrite rules for feeds
- //-------------------------------------------------------------------
- function feed_dir_rewrite( $wp_rewrite ) {
- $feed_rules = array(
- 'index.rdf' => 'index.php?feed=rdf',
- 'index.xml' => 'index.php?feed=rss2',
- '(.+).xml' => 'index.php?feed=' . $wp_rewrite->preg_index(1)
- );
- $wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;
- return $wp_rewrite->rules;
- }
- // Hook in.
- add_filter( 'generate_rewrite_rules', 'feed_dir_rewrite' );
- ?>
- <?php
- //-------------------------------------------------------------------
- // print non-wp rewrite rules
- //-------------------------------------------------------------------
- $wp_rewrite->non_wp_rules = array( 'Pattern' => 'Substitution' );
- print_r($wp_rewrite->mod_rewrite_rules());
- ?>
- prints
- <IfModule mod_rewrite.c>
- RewriteEngine On
- RewriteBase /wp_home/
- RewriteRule ^Pattern /wp_home/Substitution [QSA,L]
- RewriteCond %{REQUEST_FILENAME} !-f
- RewriteCond %{REQUEST_FILENAME} !-d
- RewriteRule . /wp_home/index.php [L]
- </IfModule>
- <?php
- //-------------------------------------------------------------------
- // check if we are on singular page of certain post type
- //-------------------------------------------------------------------
- is_singular(array( 'newspaper', 'book' ));
- ?>
- <?php
- //-------------------------------------------------------------------
- // select template via template_include filter
- //-------------------------------------------------------------------
- add_filter('template_include', 'sermon_template_include');
- function sermon_template_include($template) {
- if(get_query_var('post_type') == 'wpfc_sermon') {
- if ( is_archive() || is_search() ) :
- if(file_exists(TEMPLATEDIR . '/archive-wpfc_sermon.php'))
- return TEMPLATEDIR . '/archive-wpfc_sermon.php';
- return dirname(__FILE__) . '/views/archive-wpfc_sermon.php';
- else :
- if(file_exists(TEMPLATEDIR . '/single-wpfc_sermon.php'))
- return TEMPLATEDIR . '/single-wpfc_sermon.php';
- return dirname(__FILE__) . '/views/single-wpfc_sermon.php';
- endif;
- }
- return $template;
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // select template via template_redirect
- // for custom post type
- //-------------------------------------------------------------------
- function my_template_redirect()
- {
- global $wp;
- global $wp_query;
- if ($wp->query_vars["post_type"] == "your_custom_post_type")
- {
- if (have_posts())
- {
- include(TEMPLATEPATH . '/your_custom_post_type_template.php');
- die();
- }
- else
- {
- $wp_query->is_404 = true;
- }
- }
- }
- add_action("template_redirect", 'my_template_redirect');
- ?>
- <?php
- //-------------------------------------------------------------------
- // select template via template redirect
- //-------------------------------------------------------------------
- add_action('template_redirect', 'mcd_set_template');
- function mcd_set_template() {
- $template_path = TEMPLATEPATH . '/' . "templatename.php";
- if(file_exists($template_path)){
- include($template_path);
- exit;
- }
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // usage of template_include filter
- //-------------------------------------------------------------------
- add_action('template_include', 'mcd_set_template');
- function mcd_set_template() {
- return locate_template('templatename.php');
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // select custom template for certain category with fallback
- //-------------------------------------------------------------------
- add_filter('category_template',
- 'filter_category_template');
- //--------------------------------------------
- function filter_category_template($template){
- //--------------------------------------------
- /* Get current category */
- $category = get_queried_object();
- /* Create hierarchical list of desired templates */
- $templates = array (
- 'category.php',
- 'custom-category-template.php',
- 'category-{$category->slug}.php',
- 'category-{$category->term_id}.php',
- 'index.php'
- );
- return locate_template($templates);
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // select category template according to its parent category
- //-------------------------------------------------------------------
- add_filter( 'template_include',
- 'ja_template_check' );
- //--------------------------------------------
- function ja_template_check( $template ) {
- //--------------------------------------------
- if ( is_category() ){
- $cat = get_query_var( 'cat' );
- $category_info = get_category( $cat );
- if ( $category_info->parent == 7 ){
- return get_stylesheet_directory() . '/subcategory-news.php';
- }
- }
- return $template;
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // select nav menu according to current category
- //-------------------------------------------------------------------
- // если это категория 4 или её подкатегория
- if (cat_is_ancestor_of(4, $cat) or is_category(4)) {
- wp_nav_menu( array('menu' => 'Music' ));
- } ?>
- <?php
- //-------------------------------------------------------------------
- // redirecting according to referer
- //-------------------------------------------------------------------
- if ( wp_get_referer() )
- {
- wp_safe_redirect( wp_get_referer() );
- }
- else
- {
- wp_safe_redirect( get_home_url() );
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // add custom filter to admin post list
- //-------------------------------------------------------------------
- add_action('restrict_manage_posts',
- 'scompt_restrict_manage_posts');
- //-----------------------------------------
- function scompt_restrict_manage_posts() {
- //-----------------------------------------
- ?>
- <form name="scompt_attachmentform" id="scompt_attachmentform" action="" method="get">
- <fieldset>
- <legend><?php _e('Attachments'); ?>…</legend>
- <select name='scompt_attachments' id='scompt_attachments' class='postform'>
- <option value=""><?php _e('All'); ?></option>
- <option value="has"><?php _e('Has attachments'); ?></option>
- <option value="hasnt"><?php _e('Has no attachments'); ?></option>
- </select>
- <input type="submit" name="submit" value="<?php _e('Filter') ?>" class="button" />
- </fieldset>
- </form>
- <?php
- }
- //-----------------------------------------
- add_filter('posts_where',
- 'scompt_posts_where');
- //-----------------------------------------
- function scompt_posts_where($where) {
- //-----------------------------------------
- if( is_admin() ) {
- global $wpdb;
- if( $_GET['scompt_attachments'] == 'has' ) {
- $where .=
- " AND ID IN (SELECT post_parent FROM {$wpdb->posts} WHERE post_type='attachment' )";
- } else if( $_GET['scompt_attachments'] == 'hasnt' ) {
- $where .=
- " AND ID NOT IN (SELECT post_parent FROM {$wpdb->posts} WHERE post_type='attachment' )";
- }
- }
- return $where;
- }
- //------------------------------------------------------
- // also count posts with and without attachments
- //------------------------------------------------------
- add_action('restrict_manage_posts',
- 'scompt_restrict_manage_posts');
- //-----------------------------------------
- function scompt_restrict_manage_posts() {
- //-----------------------------------------
- global $wpdb;
- $has_count = $wpdb->get_var(
- "SELECT COUNT(*) FROM wp_posts WHERE post_type='post'
- AND ID IN (SELECT post_parent FROM wp_posts WHERE post_type='attachment')");
- $hasnt_count = $wpdb->get_var(
- "SELECT COUNT(*) FROM wp_posts WHERE post_type='post'
- AND ID NOT IN (SELECT post_parent FROM wp_posts WHERE post_type='attachment')");
- ?>
- <form name="scompt_attachmentform" id="scompt_attachmentform" action="" method="get">
- <fieldset style="margin: 0pt 1em 1em 1.5em; padding: 0pt; float: left;">
- <legend><?php _e('Attachments'); ?>…</legend>
- <select name='scompt_attachments' id='scompt_attachments' class='postform'>
- <option value=""><?php _e('All'); ?></option>
- <option value="has" <?php if( isset($_GET['scompt_attachments']) &&
- $_GET['scompt_attachments']=='has')
- echo 'selected="selected"' ?>><?php _e('Has attachments'); ?>
- (<?php echo $has_count ?>)</option>
- <option value="hasnt" <?php if( isset($_GET['scompt_attachments']) &&
- $_GET['scompt_attachments']=='hasnt') echo 'selected="selected"' ?>>
- <?php _e('Has no attachments'); ?> (<?php echo $hasnt_count ?>)</option>
- </select>
- </fieldset>
- <input type="submit" name="submit" value="<?php _e('Filter & #187;');
- ?>" class="button" style="float:left;margin:14px 0pt 1em;position:relative;top:0.35em;"/>
- </form>
- <?php
- }
- //-----------------------------------------
- add_action('admin_head',
- 'scompt_admin_head');
- //-----------------------------------------
- function scompt_admin_head() {
- //-----------------------------------------
- add_filter('posts_where', 'scompt_posts_where');
- }
- //-----------------------------------------
- function scompt_posts_where($where) {
- //-----------------------------------------
- global $wpdb;
- if( $_GET['scompt_attachments'] == 'has' ) {
- $where .= " AND ID IN
- (SELECT post_parent FROM {$wpdb->posts} WHERE post_type='attachment' )";
- } else if( $_GET['scompt_attachments'] == 'hasnt' ) {
- $where .= " AND ID NOT IN
- (SELECT post_parent FROM {$wpdb->posts} WHERE post_type='attachment' )";
- }
- return $where;
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // add filters by custom taxonomies for custom post type
- //-------------------------------------------------------------------
- //-------------------------------------------
- function work_restrict_manage_posts() {
- //-------------------------------------------
- global $typenow;
- $args=array( 'public' => true, '_builtin' => false );
- $post_types = get_post_types($args);
- if ( in_array($typenow, $post_types) ) {
- $filters = get_object_taxonomies($typenow);
- foreach ($filters as $tax_slug) {
- $tax_obj = get_taxonomy($tax_slug);
- wp_dropdown_categories(array(
- 'show_option_all' => __('Alle '.$tax_obj->label ),
- 'taxonomy' => $tax_slug,
- 'name' => $tax_obj->name,
- 'orderby' => 'term_order',
- 'selected' => $_GET[$tax_obj->query_var],
- 'hierarchical' => $tax_obj->hierarchical,
- 'show_count' => false,
- 'hide_empty' => true
- ));
- }
- }
- }
- //-------------------------------------------
- function work_convert_restrict($query) {
- //-------------------------------------------
- global $pagenow;
- global $typenow;
- if ($pagenow=='edit.php') {
- $filters = get_object_taxonomies($typenow);
- foreach ($filters as $tax_slug) {
- $var = &$query->query_vars[$tax_slug];
- if ( isset($var) && $var>0) {
- $term = get_term_by('id',$var,$tax_slug);
- $var = $term->slug;
- }
- }
- }
- return $query;
- }
- //--------------------------------------
- add_action( 'restrict_manage_posts',
- 'work_restrict_manage_posts' );
- //--------------------------------------
- add_filter('parse_query',
- 'work_convert_restrict');
- ?>
- <?php
- //-------------------------------------------------------------------
- // add sort by meta field for custom post type
- //-------------------------------------------------------------------
- add_action('restrict_manage_posts',
- 'restrict_manage_movie_sort_by_genre');
- //-------------------------------------------------
- function restrict_manage_movie_sort_by_genre() {
- //-------------------------------------------------
- if (isset($_GET['post_type'])) {
- $post_type = $_GET['post_type'];
- if (post_type_exists($post_type) && $post_type=='movie') {
- global $wpdb;
- $sql=<<<SQL
- SELECT pm.meta_key FROM {$wpdb->postmeta} pm
- INNER JOIN {$wpdb->posts} p ON p.ID=pm.post_id
- WHERE p.post_type='movie' AND pm.meta_key='Genre'
- GROUP BY pm.meta_key
- ORDER BY pm.meta_key
- SQL;
- $results = $wpdb->get_results($sql);
- $html = array();
- $html[] = "<select id=\"sortby\" name=\"sortby\">";
- $html[] = "<option value=\"None\">No Sort</option>";
- $this_sort = $_GET['sortby'];
- foreach($results as $meta_key) {
- $default = ($this_sort==$meta_key->meta_key ? ' selected="selected"' : '');
- $value = esc_attr($meta_key->meta_key);
- $html[] = "<option value=\"{$meta_key->meta_key}\"$default>{$value}</option>";
- }
- $html[] = "</select>";
- echo "Sort by: " . implode("\n",$html);
- }
- }
- }
- //--------------------------------------------
- add_filter( 'parse_query',
- 'sort_movie_by_meta_value' );
- //-------------------------------------------------
- function sort_movie_by_meta_value($query) {
- //-------------------------------------------------
- global $pagenow;
- if (is_admin() && $pagenow=='edit.php' &&
- isset($_GET['post_type']) && $_GET['post_type']=='movie' &&
- isset($_GET['sortby']) && $_GET['sortby'] !='None') {
- $query->query_vars['orderby'] = 'meta_value';
- $query->query_vars['meta_key'] = $_GET['sortby'];
- }
- }
- //-------------------------------------------------
- add_action('manage_movie_posts_columns',
- 'manage_movie_posts_columns');
- //-----------------------------------------------------
- function manage_movie_posts_columns($posts_columns) {
- //-----------------------------------------------------
- $posts_columns = array(
- 'cb' => $posts_columns['cb'],
- 'title' => 'Movie Name',
- );
- if (isset($_GET['sortby']) && $_GET['sortby'] !='None')
- $posts_columns['meta_value'] = 'Sorted By';
- return $posts_columns;
- }
- //-----------------------------------------------------
- add_action('manage_pages_custom_column',
- 'manage_movie_pages_custom_column',10,2);
- //---------------------------------------------------------------
- function manage_movie_pages_custom_column(
- $column_name,$post_id) {
- //---------------------------------------------------------------
- global $pagenow;
- $post = get_post($post_id);
- if ($post->post_type=='movie' && is_admin() && $pagenow=='edit.php') {
- switch ($column_name) {
- case 'meta_value':
- if (isset($_GET['sortby']) && $_GET['sortby'] !='None') {
- echo get_post_meta($post_id,$_GET['sortby'],true);
- }
- break;
- }
- }
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // Expand Media Library admin panel
- //-------------------------------------------------------------------
- add_action('admin_init', 'init_media_plus');
- //------------------------------------
- function init_media_plus() {
- //------------------------------------
- // Only activate plugin for the Media Library page
- //------------------------------------
- if (strpos($_SERVER["REQUEST_URI"], "upload.php") === FALSE)
- return;
- //---------------------------------
- add_action('restrict_manage_posts',
- 'media_library_menu');
- //---------------------------------
- add_action('admin_head',
- 'media_plus_header', 51);
- //---------------------------------
- add_filter('wp_redirect',
- 'media_plus_redirect', 10, 2);
- }
- //------------------------------------
- function media_library_menu() {
- //------------------------------------
- global $wpdb;
- ?>
- <div class="wrap">
- <?php screen_icon(); ?>
- <h2>Media Library Plus</h2>
- <form id="media-plus-form" action="" method="get">
- <select name="action" class="select-action">
- <option value="-1" selected="selected"><?php _e('Bulk Actions'); ?></option>
- <option value="delete"><?php _e('Delete'); ?></option>
- <option value="attach"><?php _e('Attach to a post'); ?></option>
- <option value="remove"><?php _e('Detach from a post'); ?></option>
- </select>
- <input type="submit" value="<?php esc_attr_e('Apply'); ?>"
- name="doaction" id="doaction" class="button-secondary action"
- onClick="getSelectedMedia('posts-filter');" />
- <?php wp_nonce_field('bulk-media'); ?>
- <?php find_posts_div(); ?>
- </form>
- </div> <!-- End div wrap -->
- <?php
- }
- //------------------------------------
- function media_plus_header() {
- //------------------------------------
- ?>
- <script type="text/javascript">
- <!--
- // get_selected_media(document.posts-filter.list)
- function addNewArg(name, value) {
- //-----------------------------------
- var newArg = document.createElement("input");
- newArg.type = "hidden";
- newArg.name = name;
- newArg.value = value;
- newArg.id = name;
- return newArg;
- }
- function getSelectedMedia() {
- //------------------------------------
- var mediaPlusForm=document.getElementById('media-plus-form' );
- var args = document.getElementById('posts-filter').elements;
- for (i = 0; i < args.length; i++) {
- if (args[i].name == "media[]" && args[i].checked) {
- mediaPlusForm.appendChild(addNewArg("media[]", args[i].value));
- }
- }
- // Redirect to previous page by adding previous mime_type and detached state
- var hasType = location.href.indexOf('post_mime_type');
- if (hasType >= 0) {
- var sPos = location.href.indexOf('=', mimeType);
- var ePos = location.href.indexOf('&', sPos);
- if (ePos >= 0) {
- var mimeStr = location.href.substring(sPos+1, ePos);
- } else {
- var mimeStr = location.href.substring(sPos+1);
- }
- mediaPlusForm.appendChild(addNewArg('post_mime_type', mimeStr));
- }
- if (location.href.indexOf('detached') >= 0) {
- mediaPlusForm.appendChild(addNewArg('detached', '1'));
- }
- }
- //-->
- </script>
- <?php
- }
- // Change redirect set in upload.php
- //----------------------------------------------------
- function media_plus_redirect($location, $status) {
- //----------------------------------------------------
- if ( isset($_GET['found_post_id']) && isset($_GET['media']) ) {
- if (!isset($_GET['detached']))
- $location = remove_query_arg('detached', $location);
- }
- return $location;
- }
- //----------------------------------------------------
- function detach_media_action() {
- //----------------------------------------------------
- // $_GET['doaction'] should be 'Apply'
- global $wpdb;
- if ( !strcmp($_GET['action'], 'remove') && isset($_GET['media']) ) {
- check_admin_referer('bulk-media');
- $attach = array();
- foreach( (array) $_GET['media'] as $att_id ) {
- $att_id = (int) $att_id;
- if ( !current_user_can('edit_post', $att_id) )
- continue;
- $attach[] = $att_id;
- }
- if ( ! empty($attach) ) {
- $attach = implode(',', $attach);
- $attached = $wpdb->query( $wpdb->prepare(
- "UPDATE $wpdb->posts SET post_parent = %d
- WHERE post_type = 'attachment' AND ID IN ($attach)", '') );
- }
- if ( isset($attached) ) {
- $location = 'upload.php';
- if ( $referer = wp_get_referer() ) {
- if ( false !== strpos($referer, 'upload.php') )
- $location = $referer;
- }
- $location = add_query_arg( array( 'attached' => $attached ) , $location );
- // redirect after header here can't use wp_redirect($location);
- ?>
- <script type="text/javascript">
- <!--
- window.location= <?php echo "'" . $location . "'"; ?>;
- //-->
- </script>
- <?php
- exit;
- }
- }
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // altering add_media screen
- //-------------------------------------------------------------------
- //----------------------------------------
- add_action('wp_enqueue_scripts',
- array($this, 'add_js_css'));
- //----------------------------------------
- add_filter('attachment_fields_to_edit',
- array($this, 'attachment_fields'), 15, 2);
- //--------------------------------------------------
- public function attachment_fields($form_fields) {
- //--------------------------------------------------
- global $post;
- $file = wp_get_attachment_url($post->ID);
- unset($form_fields['post_excerpt']);
- unset($form_fields['post_content']);
- unset($form_fields['url']['helps']);
- $form_fields['url']['label'] = 'URL';
- $form_fields['url']['html'] = "<input type='text' class='text urlfield'
- name='attachments[$post->ID][url]' value='" . esc_attr($file) . "' /><br />";
- $form_fields['buttons'] = array(
- 'label' => '',
- 'value' => '',
- 'html' => "<input type='submit' class='button'
- name='send[$post->ID]' value='" . esc_attr__( 'Add MP3' ) . "' />",
- 'input' => 'html'
- );
- return $form_fields;
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // remove tabs from add_media screen
- //-------------------------------------------------------------------
- add_filter('media_upload_tabs',
- 'remove_media_library_tab');
- //--------------------------------------------
- function remove_media_library_tab($tabs) {
- //--------------------------------------------
- if (isset($_REQUEST['post_id'])) {
- $post_type = get_post_type($_REQUEST['post_id']);
- if ('post' == $post_type)
- unset($tabs['library']);
- }
- return $tabs;
- }
- //----------------------------------------------
- add_filter( 'media_upload_tabs',
- 'no_media_library_tab' );
- //--------------------------------------------
- function no_media_library_tab( $tabs ) {
- //--------------------------------------------
- unset($tabs['library']);
- return $tabs;
- }
- //--------------------------------------------
- add_filter('media_upload_tabs',
- 'no_library_tab');
- //--------------------------------------------
- function no_library_tab($tabs) {
- //--------------------------------------------
- if (!current_user_can('promote_users')) {
- unset($tabs['library']);
- }
- return $tabs;
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // add custom tab to add_media screen
- //-------------------------------------------------------------------
- add_filter('media_upload_tabs',
- 'upload_test_media_menu');
- //------------------------------------------
- function upload_test_media_menu($tabs) {
- //------------------------------------------
- $tabs['test_upload']='Test Upload';
- return $tabs;
- }
- //--------------------------------------------------
- // Needed script to make sure
- // wordpresses media upload scripts are inplace
- //--------------------------------------------------
- wp_enqueue_script('media-upload');
- //---------------------------------------------
- add_action('media_upload_test_upload',
- 'upload_test_menu_handle');
- //---------------------------------------------
- // This is our form for the plugin
- //---------------------------------------------
- function upload_test_upload_handle () {
- //---------------------------------------------
- media_upload_header();
- ?>
- <div class="test-form">
- Here's some stuff in our plugin
- </div>
- <?php }
- {
- return wp_iframe('upload_test_upload_form');
- }
- //---------------------------------------------
- function upload_test_upload_form () {
- //---------------------------------------------
- media_upload_header();
- ?>
- <div class="test-form">
- <input type='text' id='name' />
- <input id='insert_shortcode' type='button' class='button' value='Insert Shortcode'>
- </div>
- <?php }
- //---------------------------------------------
- function upload_test_scripts() {
- //---------------------------------------------
- $myStyleUrl = plugins_url('upload-media.css', __FILE__);
- wp_register_style('myStyleSheets', $myStyleUrl);
- wp_enqueue_style( 'myStyleSheets');
- wp_deregister_script('jquery');
- wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
- wp_enqueue_script( 'jquery' );
- wp_deregister_script( 'upload-media-script' );
- wp_register_script( 'upload-media-script', '/wp-content/plugins/upload-media/upload-media.js');
- wp_enqueue_script( 'upload-media-script' );
- }
- ?>
- //------------------------------------
- upload_media.js
- //------------------------------------
- (function ($) {
- $(function () {
- console.log('Our Plugin Works')
- });
- })(jQuery);
- (function ($) {
- insertShortcode = function(name) {
- var win = window.dialogArguments || opener || parent || top;
- var shortcode='[testcode name='+name+']';
- win.send_to_editor(shortcode);
- }
- $(function () {
- $('#insert_shortcode').bind('click',function() {
- var name = $('#name').val();
- insertShortcode(name);});
- });
- })(jQuery);
- <?php
- //-------------------------------------------------------------------
- // Removes specified media tabs from the media uploader.
- //-------------------------------------------------------------------
- add_filter( 'media_upload_tabs', 'cwwp_remove_media_tabs' );
- //----------------------------------------------
- function cwwp_remove_media_tabs( $tabs ) {
- //----------------------------------------------
- unset( $tabs['type'] ); // This removes the "From Computer" tab
- unset( $tabs['type_url'] ); // This removes the "From URL" tab
- unset( $tabs['gallery'] ); // This removes the "Gallery" tab
- unset( $tabs['library'] ); // This remove the "Media Library" tab
- return $tabs;
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // show list of terms in custom taxonomy for post
- //-------------------------------------------------------------------
- $product_terms = wp_get_object_terms($post->ID, 'product');
- if(!empty($product_terms)){
- if(!is_wp_error( $product_terms )){
- echo '<ul>';
- foreach($product_terms as $term){
- echo '<li><a href="'.get_term_link($term->slug, 'product').'">'.$term->name.'</a></li>';
- }
- echo '</ul>';
- }
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // redirect 404 error page to custom template
- //-------------------------------------------------------------------
- //----------------------------------------------
- function loadWordPressTemplate($template) {
- //----------------------------------------------
- global $wp_query;
- if ($wp_query->is_404) {
- $wp_query->is_404 = false;
- $wp_query->is_archive = true;
- }
- header("HTTP/1.1 200 OK");
- include($template);
- exit;
- }
- //----------------------------------------------
- function templateRedirect() {
- //----------------------------------------------
- global $wp;
- if ($wp->request == 'some-custom-url-that-does-not-exist') {
- loadWordPressTemplate(TEMPLATEPATH . '/super-custom-template.php');
- }
- }
- //----------------------------------------------
- add_action('template_redirect',
- 'templateRedirect');
- ?>
- <?php
- //-------------------------------------------------------------------
- // create popup window using thickbox
- //-------------------------------------------------------------------
- add_action('init', 'init_theme_method');
- function init_theme_method() {
- add_thickbox();
- }
- ?>
- <div style="text-align:center;padding:20px;">
- <input alt="#TB_inline?height=300&width=400&inlineId=examplePopup1"
- title="add a caption to title attribute / or leave blank"
- class="thickbox" type="button"
- value="Show Thickbox Example Pop-up 1" />
- </div>
- <div id="examplePopup1" style="display:none">
- <h2>Example Pop-up Window 1</h2>
- <div style="float:left;padding:10px;">
- <img src="http://shibashake.com/wordpress-theme/wp-content/uploads/2010/03/bio1.jpg"
- width="150" height = "168"/>
- </div>
- I was born at DAZ Studio.
- They created me with utmost care and that is
- why I am the hottie that you see today.
- My interests include posing, trying out cute outfits,
- and more posing.
- <select name=""><option>test</option></select>
- <strong>Just click outside the pop-up to close it.</strong>
- </div>
- //--------------------------------------------------
- add_action('template_redirect', 'add_scripts');
- //-------------------------------------
- function add_scripts() {
- //-------------------------------------
- if (is_single('2794')) {
- add_thickbox();
- }
- }
- //-------------------------------------
- function add_scripts() {
- //-------------------------------------
- global $wp_query;
- if (!is_singular() || !$wp_query->post) return;
- //------------------------
- // Get post tags
- //------------------------
- $tags = wp_get_object_terms($wp_query->post->ID, 'post_tag');
- foreach ($tags as $tag) {
- if ($tag->name == "thickbox") {
- add_thickbox();
- break;
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement