Advertisement
terorama

Wordpress Snippets 7

Oct 15th, 2013
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 62.09 KB | None | 0 0
  1.  
  2. <?php  
  3. //-------------------------------------------------------------------
  4. //  Modifies the default method for injecting media into posts  
  5. //-------------------------------------------------------------------
  6.  
  7. //-----------------------------------
  8. class ewmInsertMedia {  
  9. //-----------------------------------
  10.    
  11.  
  12.   //----------------------------
  13.   function ewmInsertMedia () {
  14.   //----------------------------
  15.  
  16.     add_filter('media_send_to_editor',
  17.  
  18.                     array($this,
  19.                           modifyMediaInsert) , 10, 3);  
  20.   }  
  21.  
  22.   //----------------------------
  23.   function modifyMediaInsert(
  24.  
  25.                   $html, $send_id, $attachment) {
  26.  
  27.   //----------------------------  
  28.     $output = '[flash '.$attachment['url'].']';  
  29.     return $output;  
  30.   }  
  31. }  
  32. //instantiate the class  
  33. $ewmIM = new ewmInsertMedia();  
  34. ?>  
  35.  
  36. <?php
  37. //-------------------------------------------------------------------
  38. //-------------------------------------------------------------------
  39.  
  40. //----------------------------------
  41. // Add to admin_menu action
  42. //----------------------------------        
  43. $my_page = add_media_page('Shiba Gallery', 'Shiba Gallery',
  44.                  
  45.                         'administrator', 'shiba_gallery', 'my_plugin_page' );
  46.  
  47.  
  48. add_action("admin_print_scripts-{$my_page}",
  49.                                           'my_plugin_page_scripts');
  50.  
  51. add_action("admin_print_styles-{$my_page}",
  52.                                             'my_plugin_page_styles');
  53.  
  54. //-----------------------------------
  55. function my_plugin_page_styles() {
  56. //-----------------------------------
  57.     wp_enqueue_style('thickbox');
  58. }
  59.  
  60. //-----------------------------------
  61. function my_plugin_page_scripts() {
  62. //-----------------------------------
  63.     wp_enqueue_script('thickbox');
  64. }
  65.  
  66. //-----------------------------------
  67.  
  68. $image_library_url = get_upload_iframe_src( 'image', null, 'library' );
  69. $image_library_url = remove_query_arg( array('TB_iframe'), $image_library_url );
  70.  
  71. $image_library_url = add_query_arg(
  72.  
  73.            array( 'context' =>
  74.                       'shiba-gallery-default-image',
  75.  
  76.                  'TB_iframe' => 1 ),
  77.  
  78.                $image_library_url );
  79. ?>
  80.          
  81. <p>
  82. <a title="Set default image" href="<?php
  83.           echo esc_url( $image_library_url ); ?>" id="set-default-image"
  84.          class="button thickbox">Set Default Image</a>
  85.  
  86. </p>
  87. <?php
  88. //---------------------------------------------
  89. add_filter('media_upload_tabs',
  90.  
  91.                          'my_plugin_image_tabs', 10, 1);
  92.  
  93. //---------------------------------------------
  94. function my_plugin_image_tabs(
  95.                            $_default_tabs) {
  96. //---------------------------------------------
  97.     unset($_default_tabs['type']);
  98.     unset($_default_tabs['type_url']);
  99.     unset($_default_tabs['gallery']);
  100.          
  101.     return($_default_tabs);
  102. }
  103.  
  104. //---------------------------------------------
  105. add_filter('attachment_fields_to_edit',
  106.                                 'my_plugin_action_button', 20, 2);
  107.  
  108. //---------------------------------------------
  109. add_filter('media_send_to_editor',
  110.                                 'my_plugin_image_selected', 10, 3);
  111.  
  112. //---------------------------------------------
  113. function my_plugin_action_button(
  114.                        $form_fields, $post) {
  115. //---------------------------------------------
  116.  
  117.     $send = "<input type='submit' class='button' name='send[$post->ID]'
  118.                value='" . esc_attr__( 'Use as Default' ) . "' />";
  119.  
  120.     $form_fields['buttons'] = array('tr' =>
  121.  
  122.                             "\t\t<tr class='submit'><td>
  123.                            </td><td class='savesend'>$send</td></tr>\n");
  124.  
  125.  
  126.     $form_fields['context'] = array( 'input' => 'hidden',
  127.                               'value' => 'shiba-gallery-default-image' );
  128.  
  129.     return $form_fields;
  130. }
  131.  
  132. //---------------------------------------------
  133. function my_plugin_image_selected(
  134.  
  135.               $html, $send_id, $attachment) {
  136. //---------------------------------------------
  137.     ?>
  138.     <script type="text/javascript">
  139.  
  140.     /* <![CDATA[ */
  141.  
  142.     var win = window.dialogArguments || opener || parent || top;
  143.                  
  144.     win.jQuery( '#default_image' ).val('<?php echo $send_id;?>');
  145.  
  146.     //-----------------------submit the form
  147.  
  148.     win.jQuery( '#shiba-gallery_options' ).submit();
  149.  
  150.     /* ]]> */
  151.     </script>
  152.  
  153.     <?php
  154.     exit();
  155. }
  156. //-------------------------------------------------------------------
  157. //-------------------------------------------------------------------
  158. //---------------------------------------------
  159. // Add to admin_menu
  160. //---------------------------------------------
  161. if (check_upload_image_context(
  162.                     'shiba-gallery-default-image')) {
  163.  
  164.         add_filter('media_upload_tabs',
  165.                                       'my_plugin_image_tabs', 10, 1);
  166.  
  167.         add_filter('attachment_fields_to_edit',
  168.                                       'my_plugin_action_button', 20, 2);
  169.  
  170.         add_filter('media_send_to_editor',
  171.                                       'my_plugin_image_selected', 10, 3);
  172. }
  173.  
  174. //---------------------------------------------
  175. function add_my_context_to_url($url, $type) {
  176. //---------------------------------------------
  177.     if ($type != 'image') return $url;
  178.     if (isset($_REQUEST['context'])) {
  179.  
  180.         $url = add_query_arg('context', $_REQUEST['context'], $url);
  181.     }
  182.     return $url;    
  183. }
  184. //---------------------------------------------    
  185. function check_upload_image_context($context) {
  186. //---------------------------------------------
  187.  
  188.     if (isset($_REQUEST['context']) && $_REQUEST['context'] == $context) {
  189.  
  190.         add_filter('media_upload_form_url',
  191.                          array($this,
  192.                                  'add_my_context_to_url'), 10, 2);
  193.         return TRUE;
  194.     }
  195.     return FALSE;
  196. }
  197. ?>
  198.  
  199. <?php
  200. //-------------------------------------------------------------------
  201. //        Creating custom fields for attachment
  202. //-------------------------------------------------------------------
  203.  
  204. //---------------------------------------------
  205. //             system hooks
  206. //---------------------------------------------
  207. function get_attachment_fields_to_edit(
  208.                      $post, $errors = null) {  
  209.     // ...  
  210.     $form_fields = apply_filters("attachment_fields_to_edit", $form_fields, $post);  
  211.     // ...  
  212. }  
  213. //---------------------------------------------
  214. function media_upload_form_handler() {  
  215.     // ...  
  216.     $post = apply_filters("attachment_fields_to_save", $post, $attachment);  
  217.     // ...  
  218. }  
  219.  
  220.  
  221. //---------------------------------------------
  222. //---------------------------------------------
  223. add_filter("attachment_fields_to_edit",
  224.  
  225.                        "my_image_attachment_fields_to_edit", null, 2);
  226.  
  227. /**----------------------------------------------------
  228.  * Adding our custom fields to the $form_fields array
  229.  *  
  230.  * @param array $form_fields
  231.  * @param object $post
  232.  * @return array
  233.  -----------------------------------------------------*/  
  234. function my_image_attachment_fields_to_edit(
  235.  
  236.                                $form_fields, $post) {  
  237.     //----------------------------------------------------
  238.     // $form_fields is a special array of fields
  239.     //  to include in the attachment form  
  240.     // $post is the attachment record in the database  
  241.     //     $post->post_type == 'attachment'  
  242.     // (attachments are treated as posts in WordPress)  
  243.      
  244.     // add our custom field to the $form_fields array  
  245.  
  246.     // input type="text"
  247.     //    name/id="attachments[$attachment->ID][custom1]"  
  248.  
  249.     //----------------------------------------------------
  250.  
  251.     $form_fields["custom1"] = array(  
  252.         "label" => __("Custom Text Field"),  
  253.         "input" => "text", // this is default if "input" is omitted  
  254.         "value" => get_post_meta($post->ID, "_custom1", true)  
  255.     );  
  256.  
  257.     //-------------------------------------------------------------
  258.     // if you will be adding error messages for your field,  
  259.     // then in order to not overwrite them, as they are pre-attached  
  260.     // to this array, you would need to set the field up like this:  
  261.     //-------------------------------------------------------------
  262.  
  263.     $form_fields["custom1"]["label"] = __("Custom Text Field");  
  264.     $form_fields["custom1"]["input"] = "text";  
  265.     $form_fields["custom1"]["value"] = get_post_meta($post->ID, "_custom1", true);  
  266.      
  267.     return $form_fields;  
  268. }  
  269.  
  270. //--------------------------------------------
  271. //          text input
  272. //--------------------------------------------
  273. $form_fields["custom1"]["label"] = __("Custom Text Field");  
  274. $form_fields["custom1"]["input"] = "text"; // this is default if "input" is omitted  
  275. $form_fields["custom1"]["value"] = get_post_meta($post->ID, "_custom1", true);  
  276.  
  277. //--------------------------------------------
  278. //         text area
  279. //--------------------------------------------
  280. $form_fields["custom2"]["label"] = __("Custom Textarea");  
  281. $form_fields["custom2"]["input"] = "textarea";  
  282. $form_fields["custom2"]["value"] = get_post_meta($post->ID, "_custom2", true);
  283.  
  284. //--------------------------------------------
  285. //       hidden field
  286. //--------------------------------------------
  287. $form_fields["custom3"]["input"] = "hidden";  
  288. $form_fields["custom3"]["value"] = get_post_meta($post->ID, "_custom3", true);  
  289.  
  290. //--------------------------------------------
  291. //            select
  292. //--------------------------------------------
  293. $form_fields["custom4"]["label"] = __("Custom Select");  
  294. $form_fields["custom4"]["input"] = "html";  
  295. $form_fields["custom4"]["html"] = "
  296. <select name='attachments[{$post->ID}][custom4]' id='attachments[{$post->ID}][custom4]'>
  297.    <option value='1'>Option 1</option>
  298.    <option value='2'>Option 2</option>
  299.    <option value='3'>Option 3</option>
  300. </select>";  
  301.  
  302. //--------------------------------------------
  303. //           checkbox
  304. //--------------------------------------------
  305. $form_fields["custom5"]["label"] = __("Custom Checkbox");  
  306. $form_fields["custom5"]["input"] = "html";  
  307. $form_fields["custom5"]["html"] = "the html output goes here, like a checkbox:  
  308. <input type='checkbox' value='1'  
  309.    name='attachments[{$post->ID}][custom5]'  
  310.    id='attachments[{$post->ID}][custom5]' />";  
  311.  
  312.  
  313. //--------------------------------------------
  314. //             helps attribute
  315. //--------------------------------------------
  316. $form_fields["custom6"]["label"] = __("Custom Field with Helpful Text");  
  317. $form_fields["custom6"]["value"] = get_post_meta($post->ID, "_custom6", true);  
  318. $form_fields["custom6"]["helps"] = "Put helpful text here.";  
  319.  
  320.  
  321. //--------------------------------------------
  322. //          required attribute
  323. //--------------------------------------------
  324. $form_fields["custom7"]["label"] = __("Required Field");  
  325. $form_fields["custom7"]["value"] = get_post_meta($post->ID, "_custom7", true);  
  326. $form_fields["custom7"]["required"] = TRUE; // default is FALSE
  327.  
  328. //--------------------------------------------
  329. //         extra rows attribute
  330. //--------------------------------------------
  331. // extra_rows markup:  
  332. // <tr>  
  333. //      <td></td>  
  334. //      <td class="{arrayItemKey}">{arrayItemValue}</td>  
  335. // </tr>  
  336.  
  337. $form_fields["custom8"]["label"] = __("Custom Field with Extra Rows");  
  338. $form_fields["custom8"]["value"] = get_post_meta($post->ID, "_custom8", true);  
  339.  
  340. // extra_rows must be an associative array $cssClass => $content  
  341.  
  342. $form_fields["custom8"]["extra_rows"] = array(  
  343.     "cssClass1" => "If you need a few rows after your field...",  
  344.     "cssClass2" => "...to maybe explain something or add some imagery?
  345.            <img src='".get_bloginfo("wpurl")."/wp-admin/images/align-left.png' />
  346.            <img src='".get_bloginfo("wpurl")."/wp-admin/images/align-center.png' />
  347.            <img src='".get_bloginfo("wpurl")."/wp-admin/images/align-right.png' />",  
  348. );  
  349.  
  350. //--------------------------------------------
  351. //              tr attribute
  352. //--------------------------------------------
  353. $form_fields["custom8"]["tr"] = "
  354. <tr id='MySpecialRow'>
  355.    <td colspan='2' style='background:#000;color:#fff;'>
  356.        Can do whatever you want, style it, add some fields, display a table of data...sky's the limit
  357.    </td>
  358. </tr>";  
  359.  
  360.  
  361.  
  362.  
  363. /**----------------------------------------------------------------
  364.  * @param array $post
  365.  * @param array $attachment
  366.  * @return array
  367.  -----------------------------------------------------------------*/  
  368. function my_image_attachment_fields_to_save(
  369.  
  370.                                $post, $attachment) {  
  371. //------------------------------------------------------------------
  372.  
  373.     //--------------------------------------------------------------
  374.     // $attachment part of the form $_POST
  375.     //                           ($_POST[attachments][postID])  
  376.  
  377.     // $post attachments wp post array - will be saved after returned  
  378.     //     $post['post_type'] == 'attachment'  
  379.     //--------------------------------------------------------------
  380.  
  381.     if( isset($attachment['my_field']) ){  
  382.        
  383.         update_post_meta($post['ID'], '_my_field', $attachment['my_field']);  
  384.     }  
  385.     return $post;  
  386. }  
  387.  
  388. //----------------------------------------------------------
  389. function my_image_attachment_fields_to_save(
  390.                              $post, $attachment) {  
  391. //----------------------------------------------------------
  392.     if( isset($attachment['my_field']) ){  
  393.  
  394.         if( trim($attachment['my_field']) == '' ){  
  395.  
  396.             //------------------------------
  397.             // adding our custom error  
  398.             //------------------------------
  399.             $post['errors']['my_field']['errors'][] = __('Error text here.');  
  400.         }else{  
  401.             update_post_meta($post['ID'], 'my_field', $attachment['my_field']);  
  402.         }  
  403.     }  
  404.     return $post;  
  405. }  
  406.  
  407. ?>
  408.  
  409. <?php
  410. //-------------------------------------------------------------------
  411. //     altering the appearance of custom taxonomy inputs
  412. //         on post edit screen
  413. //-------------------------------------------------------------------
  414. add_action('admin_head','remove_bath_parents');
  415.  
  416. //--------------------------------------
  417. function remove_bath_parents() {
  418. //--------------------------------------
  419.   global $pagenow;
  420.  
  421.   //-----------------------------------------
  422.   //     Only for the post add & edit pages
  423.   //-----------------------------------------
  424.   if (in_array($pagenow,array('post-new.php','post.php'))) {
  425.  
  426.     $css=<<<STYLE
  427. <style>
  428. <!--
  429. #newbath_parent {
  430.   display:none;
  431. }
  432. -->
  433. </style>
  434. STYLE;
  435.  
  436.     echo $css;
  437.   }
  438. }
  439.  
  440. //-----------------------------------------
  441. add_action('init','add_homes_and_baths');
  442.  
  443. //-----------------------------------------
  444. function add_homes_and_baths() {
  445. //-----------------------------------------
  446.   register_post_type('home',
  447.     array(
  448.       'label'           => 'Homes',
  449.       'public'          => true,
  450.       'rewrite'         => array('slug' => 'homes'),
  451.       'hierarchical'    => false,
  452.     )
  453.   );
  454.   register_taxonomy('bath', 'home', array(
  455.     'hierarchical'    => true,
  456.     'label'           => 'Baths',
  457.     'rewrite'         => array('slug' => 'baths' ),
  458.     )
  459.   );
  460. }
  461. //-----------------------------------------
  462. add_action('add_meta_boxes',
  463.              'mysite_add_meta_boxes',10,2);
  464.  
  465. //-----------------------------------------
  466. function mysite_add_meta_boxes(
  467.                      $post_type, $post) {
  468. //-----------------------------------------
  469.   ob_start();
  470. }
  471.  
  472. //-----------------------------------------
  473. add_action('dbx_post_sidebar',
  474.               'mysite_dbx_post_sidebar');
  475.  
  476. //-----------------------------------------
  477. function mysite_dbx_post_sidebar() {
  478. //-----------------------------------------
  479.   $html = ob_get_clean();
  480.   $html = str_replace('"checkbox"','"radio"',$html);
  481.   echo $html;
  482. }
  483. ?>
  484.  
  485. <?php
  486. //-------------------------------------------------------------------
  487. //     add content above the post title on post edit screen
  488. //-------------------------------------------------------------------
  489.  
  490. function  wpse27700_above_title_content()
  491. {
  492.     ?>
  493.     <style>
  494.     /*
  495.     You might need to attach some styles here,
  496.     to not get into the admin notices styles
  497.     */
  498.     </style>
  499.  
  500.     <h1>TEST</h1>
  501.     <p>This is a test message</p>
  502.     <?php
  503. }
  504.  
  505. //-------------------------------------------
  506. // This is needed to only hook
  507. //     on the post new & edit screens.
  508. //-------------------------------------------
  509. function wpse27700_admin_head()
  510. {
  511.     add_action( 'admin_notices', 'wpse27700_above_title_content', 9999 );
  512. }
  513.  
  514. //-----------------------------------------------
  515. add_action( 'admin_head-post-new.php', 'wpse27700_admin_head' );
  516. add_action( 'admin_head-post.php', 'wpse27700_admin_head' );
  517.  
  518. ?>
  519.  
  520. <?php
  521. //-------------------------------------------------------------------
  522. //     add image below all on post edit screen
  523. //       and then move it up via jQuery
  524. //-------------------------------------------------------------------
  525.  
  526. add_action( 'dbx_post_sidebar',
  527.                       'wpse27700_add_image' );
  528.  
  529. //-----------------------------------------
  530. function wpse27700_add_image()
  531. //-----------------------------------------
  532. {
  533.     echo '<img id="wpse27700-image" src="http://placebear.com/500/100"
  534.       alt="wpse27700 bear" style="display:none;" />';
  535. }
  536.  
  537.  
  538. add_action( 'admin_head',
  539.                      'wpse27700_admin_head' );
  540.  
  541. //-----------------------------------------
  542. function wpse27700_admin_head()
  543. //-----------------------------------------
  544. {
  545.     ?>
  546.     <script type="text/javascript">
  547.         jQuery(document).ready(function(){
  548.             var wpse27700 = jQuery('#wpse27700-image');
  549.             wpse27700.remove();
  550.             jQuery('#titlediv #titlewrap').before(wpse27700.show());
  551.         });
  552.     </script>
  553.     <?php  
  554. }
  555. ?>
  556.  
  557. <?php
  558. //-------------------------------------------------------------------
  559. //         change category checkboxes to radiobuttons
  560. //                on post edit screen
  561. //-------------------------------------------------------------------
  562.  
  563. add_action('add_meta_boxes',
  564.                        'mysite_add_meta_boxes',10,2);
  565.  
  566. //-----------------------------------------
  567. function mysite_add_meta_boxes(
  568.                      $post_type, $post) {
  569. //-----------------------------------------
  570.     ob_start();
  571. }
  572.  
  573. //-----------------------------------------
  574. add_action('dbx_post_sidebar',
  575.                 'mysite_dbx_post_sidebar');
  576.  
  577. //-----------------------------------------
  578. function mysite_dbx_post_sidebar() {
  579. //-----------------------------------------
  580.     $html = ob_get_clean();
  581.     $html = str_replace('"checkbox"','"radio"',$html);
  582.     echo $html;
  583. }
  584.  
  585. ?>
  586. <?php
  587. //-------------------------------------------------------------------
  588. //       display custom taxonomy input as radiogroup
  589. //       on custom post edit screen
  590. //-------------------------------------------------------------------
  591.  
  592. class My_Radio_Tax{
  593.  
  594.     static $taxonomy = 'event-category';    // Slug of taxonomy
  595.     static $post_type = 'event';            // Post type for meta-box
  596.  
  597.     //-----------------------------------------
  598.     function load(){
  599.         add_action( 'admin_menu', array(__CLASS__,'remove_meta_box'));
  600.         add_action( 'add_meta_boxes', array(__CLASS__,'add_meta_box'));
  601.     }
  602.  
  603.     //-----------------------------------------
  604.     //Remove taxonomy meta box
  605.     //-----------------------------------------
  606.     function remove_meta_box(){
  607.     //-------------------------------------------------------
  608.         // The taxonomy metabox ID.
  609.         // This is different for non-hierarchical taxonomies
  610.         //---------------------------------------------------
  611.         $tax_mb_id = self::$taxonomy.'div';
  612.         remove_meta_box($tax_mb_id, self::$post_type, 'normal');
  613.     }
  614.  
  615.     //-----------------------------------------
  616.     //Add new taxonomy meta box
  617.     //-----------------------------------------
  618.     function add_meta_box() {
  619.         add_meta_box( 'my_tax', 'My taxonomy',array(__CLASS__,'metabox_inner'),'event' ,'side','core');
  620.     }
  621.  
  622.     //-----------------------------------------
  623.     //     Callback to set up metabox
  624.     //-----------------------------------------
  625.     function metabox_inner( $post ) {
  626.     //-----------------------------------------
  627.         //Get taxonomy and terms
  628.         //-------------------------------------
  629.         $taxonomy = self::$taxonomy;
  630.         $tax = get_taxonomy($taxonomy);
  631.         $name = 'tax_input[' . $taxonomy . ']';
  632.         $terms = get_terms('event-category',array('hide_empty' => 0));
  633.  
  634.  
  635.         //-------------------------------------
  636.         //Get current and popular terms
  637.         //-------------------------------------
  638.          $popular = get_terms( $taxonomy, array( 'orderby' => 'count', 'order' => 'DESC',
  639.                                         'number' => 10, 'hierarchical' => false ) );
  640.  
  641.         $postterms = get_the_terms( $post->ID,$taxonomy );
  642.         $current = ($postterms ? array_pop($postterms) : false);
  643.         $current = ($current ? $current->term_id : 0);
  644.         ?>
  645.  
  646.         <div id="taxonomy-<?php echo $taxonomy; ?>" class="categorydiv">
  647.  
  648.             <!-- Display tabs-->
  649.  
  650.             <ul id="<?php echo $taxonomy; ?>-tabs" class="category-tabs">
  651.  
  652.                 <li class="tabs"><a href="#<?php echo $taxonomy; ?>-all" tabindex="3"><?php
  653.                      echo $tax->labels->all_items; ?></a></li>
  654.  
  655.                 <li class="hide-if-no-js"><a href="#<?php
  656.                     echo $taxonomy; ?>-pop" tabindex="3"><?php _e( 'Most Used' ); ?></a></li>
  657.  
  658.             </ul>
  659.  
  660.             <!-- Display popular taxonomy terms -->
  661.  
  662.             <div id="<?php echo $taxonomy; ?>-pop" class="tabs-panel" style="display: none;">
  663.                 <ul id="<?php echo $taxonomy; ?>checklist-pop" class="categorychecklist form-no-clear" >
  664.  
  665.                     <?php   foreach($popular as $term){
  666.  
  667.                         $id = "id='in-popular-event-category-$term->term_id'";
  668.  
  669.                         echo "<li id='popular-event-category-$taxonomy-$term->term_id'>
  670.                                   <label class='selectit'>";
  671.  
  672.                         echo "<input type='radio' {$id} name='{$name}'".
  673.                               checked($current,$term->term_id,false).
  674.                               "value='$term->term_id' />$term->name<br />";
  675.  
  676.                         echo "</label></li>";
  677.                     }?>
  678.                 </ul>
  679.             </div>
  680.  
  681.             <!-- Display taxonomy terms -->
  682.  
  683.             <div id="<?php echo $taxonomy; ?>-all" class="tabs-panel">
  684.                 <ul id="<?php echo $taxonomy; ?>checklist" class="list:<?php
  685.                        echo $taxonomy?> categorychecklist form-no-clear">
  686.  
  687.                     <?php   foreach($terms as $term){
  688.                         $id = "id='in-event-category-$term->term_id'";
  689.  
  690.                         echo "<li id='event-category-$taxonomy-$term->term_id'>
  691.                                  <label class='selectit'>";
  692.                         echo "<input type='radio' {$id} name='{$name}'".
  693.                                 checked($current,$term->term_id,false).
  694.                                 "value='$term->term_id' />$term->name<br />";
  695.                         echo "</label></li>";
  696.                     }?>
  697.                 </ul>
  698.             </div>
  699.         </div>
  700.         <?php
  701.     }
  702. }
  703. My_Radio_Tax::load();
  704.  
  705. ?>
  706. <?php
  707. //-------------------------------------------------------------------
  708. //                     get plugin name
  709. //-------------------------------------------------------------------
  710. $x = plugin_basename(__FILE__);
  711.  
  712. ?>
  713.  
  714. <?php
  715. //-------------------------------------------------------------------
  716. //           manipulating with dashboard widgets
  717. //-------------------------------------------------------------------
  718.  
  719. class Wptuts_Dashboard_Widgets {
  720.  
  721.     function __construct() {
  722.         add_action( 'wp_dashboard_setup', array( $this, 'remove_dashboard_widgets' ) );
  723.         add_action( 'wp_dashboard_setup', array( $this, 'add_dashboard_widgets' ) );
  724.     }
  725.  
  726.     function remove_dashboard_widgets() {
  727.  
  728.     }
  729.  
  730.     function add_dashboard_widgets() {
  731.  
  732.     }
  733.  
  734. }
  735. $wdw = new Wptuts_Dashboard_Widgets();
  736. ?>
  737.  
  738. <?php
  739. //-------------------------------------------------------------------
  740. //               add and remove dashboard widgets
  741. //-------------------------------------------------------------------
  742.  
  743. $remove_defaults_widgets = array(
  744.     'dashboard_incoming_links' => array(
  745.         'page'    => 'dashboard',
  746.         'context' => 'normal'
  747.     ),
  748.     'dashboard_right_now' => array(
  749.         'page'    => 'dashboard',
  750.         'context' => 'normal'
  751.     ),
  752.     'dashboard_recent_drafts' => array(
  753.         'page'    => 'dashboard',
  754.         'context' => 'side'
  755.     ),
  756.     'dashboard_quick_press' => array(
  757.         'page'    => 'dashboard',
  758.         'context' => 'side'
  759.     ),
  760.     'dashboard_plugins' => array(
  761.         'page'    => 'dashboard',
  762.         'context' => 'normal'
  763.     ),
  764.     'dashboard_primary' => array(
  765.         'page'    => 'dashboard',
  766.         'context' => 'side'
  767.     ),
  768.     'dashboard_secondary' => array(
  769.         'page'    => 'dashboard',
  770.         'context' => 'side'
  771.     ),
  772.     'dashboard_recent_comments' => array(
  773.         'page'    => 'dashboard',
  774.         'context' => 'normal'
  775.     )
  776. );
  777.  
  778. //----------------------------------------
  779. $custom_dashboard_widgets = array(
  780.     'my-dashboard-widget' => array(
  781.         'title' => 'My Dashboard Widget',
  782.         'callback' =>
  783.                      //--------------------------
  784.                      'dashboardWidgetContent'
  785.                      //--------------------------
  786.     )
  787. );
  788.  
  789. //----------------------------------------
  790. function dashboardWidgetContent() {
  791. //----------------------------------------
  792.     $user = wp_get_current_user();
  793.  
  794.     echo "Hello <strong>" . $user->user_login .
  795.               "</strong>, this is your custom widget.
  796.                You can, for instance, list all the posts you've published:";
  797.  
  798.     $r = new WP_Query( apply_filters( 'widget_posts_args', array(
  799.         'posts_per_page' => 10,
  800.         'post_status' => 'publish',
  801.         'author' => $user->ID
  802.     ) ) );
  803.  
  804.     if ( $r->have_posts() ) :
  805.     ?>
  806.     <?php
  807.     endif;
  808. }
  809.  
  810. //----------------------------------------
  811. require_once( plugin_dir_path( __FILE__ ) . '/custom_widgets.php' );
  812.  
  813. //----------------------------------------
  814. function remove_dashboard_widgets() {
  815. //----------------------------------------
  816.     global $remove_defaults_widgets;
  817.     foreach ( $remove_defaults_widgets as $wigdet_id => $options ) {
  818.  
  819.         remove_meta_box( $wigdet_id, $options['page'], $options['context'] );
  820.     }
  821. }
  822.  
  823. //----------------------------------------
  824. function add_dashboard_widgets() {
  825. //----------------------------------------
  826.     global $custom_dashboard_widgets;
  827.  
  828.     foreach ( $custom_dashboard_widgets as $widget_id => $options ) {
  829.  
  830.         wp_add_dashboard_widget(
  831.             $widget_id,
  832.             $options['title'],
  833.             $options['callback']
  834.         );
  835.     }
  836. }
  837. ?>
  838.  
  839. <?php
  840. //-------------------------------------------------------------------
  841. //     output categories as list of checkboxes
  842. //-------------------------------------------------------------------
  843.  
  844. $selected_cats = array( 45, 33, 118 );
  845. $checked_ontop = true;
  846.  
  847. wp_category_checklist( 0, 0, $selected_cats, false, null, $checked_ontop );
  848.  
  849. ?>
  850.  
  851. <?php
  852. //-------------------------------------------------------------------
  853. //         sanitize post_title as html class name
  854. //-------------------------------------------------------------------
  855.  
  856. // If you want to explicitly style a post,
  857. // you can use the sanitized version of the post title as a class
  858.  
  859. $post_class = sanitize_html_class( $post->post_title );
  860. echo '<div class="' . esc_attr( $post_class ) . '">';
  861.  
  862.  
  863. //-------------------------------------------------------------------
  864. //    output navigation menu assigned to 'primary' menu block
  865. //-------------------------------------------------------------------
  866. ?>
  867.  
  868. <div id="access" role="navigation">
  869.  
  870.     <?php /*
  871.  
  872.     Allow screen readers / text browsers to skip the navigation menu and
  873.     get right to the good stuff. */ ?>
  874.  
  875.     <div class="skip-link screen-reader-text">
  876.         <a href="#content" title="<?php esc_attr_e( 'Skip to content', 'twentyten' ); ?>">
  877.         <?php _e( 'Skip to content', 'twentyten' ); ?></a>
  878.     </div>
  879.  
  880.     <?php /*
  881.  
  882.     Our navigation menu.  If one isn't filled out, wp_nav_menu falls
  883.     back to wp_page_menu.  The menu assigned to the primary position is
  884.     the one used.  If none is assigned, the menu with the lowest ID is
  885.     used. */
  886.  
  887.     wp_nav_menu( array( 'container_class' => 'menu-header',
  888.                               'theme_location' => 'primary' ) ); ?>
  889.  
  890. </div><!-- #access -->
  891.  
  892.  
  893. <?php
  894. //-------------------------------------------------------------------
  895. //               altering output of wp_nav_menu
  896. //-------------------------------------------------------------------
  897.  
  898. function my_wp_nav_menu_args( $args = '' ) {
  899. //---------------------------------------------
  900.     $args['container'] = false;
  901.     return $args;
  902. }
  903. //---------------------------------------------
  904. add_filter( 'wp_nav_menu_args',
  905.                             'my_wp_nav_menu_args' );
  906.  
  907. //---------------------------------------------
  908. wp_nav_menu( array( 'items_wrap' => '%3$s' ) );
  909.  
  910. //---------------------------------------------
  911. wp_nav_menu( array( 'theme_location' => 'primary',
  912.              'items_wrap' =>
  913.                   '<ul><li id="item-id">Menu: </li>%3$s</ul>' ) );
  914.  
  915. //---------------------------------------------
  916. add_filter('nav_menu_css_class' ,
  917.                  'special_nav_class' , 10 , 2);
  918.  
  919. //---------------------------------------------
  920. function special_nav_class($classes, $item){
  921. //---------------------------------------------
  922.      if(is_single() && $item->title == "Blog"){
  923.              $classes[] = "special-class";
  924.      }
  925.      return $classes;
  926. }
  927. ?>
  928.  
  929. <?php
  930. //-------------------------------------------------------------------
  931. //           use custom menu Walker class
  932. //-------------------------------------------------------------------
  933.  
  934.  
  935. class themeslug_walker_nav_menu extends Walker_Nav_Menu {
  936.  
  937. //----------------------------------------
  938. // add classes to ul sub-menus
  939. //----------------------------------------
  940. //---------------------------------------
  941. function start_lvl( &$output, $depth ) {
  942. //---------------------------------------
  943.     //-----------------------------
  944.     // depth dependent classes
  945.     //-----------------------------
  946.     $indent = ( $depth > 0  ? str_repeat( "\t", $depth ) : '' );
  947.  
  948.     $display_depth = ( $depth + 1);
  949.  
  950.     $classes = array(
  951.         'sub-menu',
  952.         ( $display_depth % 2  ? 'menu-odd' : 'menu-even' ),
  953.         ( $display_depth >=2 ? 'sub-sub-menu' : '' ),
  954.         'menu-depth-' . $display_depth
  955.         );
  956.  
  957.     $class_names = implode( ' ', $classes );
  958.  
  959.     // build html
  960.     $output .= "\n" . $indent . '<ul class="' . $class_names . '">' . "\n";
  961. }
  962. //----------------------------------------
  963. // add main/sub classes to li's and links
  964. //----------------------------------------
  965. //----------------------------------------------
  966.  function start_el(
  967.            &$output, $item, $depth, $args ) {
  968. //----------------------------------------------
  969.  
  970.     global $wp_query;
  971.     $indent = ( $depth > 0 ? str_repeat( "\t", $depth ) : '' );
  972.  
  973.     //----------------------------
  974.     // depth dependent classes
  975.     //----------------------------
  976.     $depth_classes = array(
  977.         ( $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ),
  978.         ( $depth >=2 ? 'sub-sub-menu-item' : '' ),
  979.         ( $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ),
  980.         'menu-item-depth-' . $depth
  981.     );
  982.  
  983.     $depth_class_names = esc_attr( implode( ' ', $depth_classes ) );
  984.  
  985.     //----------------------------
  986.     // passed classes
  987.     //----------------------------
  988.     $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  989.     $class_names = esc_attr( implode( ' ',
  990.  
  991.            apply_filters( 'nav_menu_css_class',
  992.                              array_filter( $classes ), $item ) ) );
  993.  
  994.     //----------------------------
  995.     // build html
  996.     //----------------------------
  997.     $output .= $indent . '<li id="nav-menu-item-'. $item->ID .
  998.                '" class="' . $depth_class_names . ' ' . $class_names . '">';
  999.  
  1000.     //----------------------------
  1001.     // link attributes
  1002.     //----------------------------
  1003.     $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
  1004.     $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
  1005.     $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
  1006.     $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';
  1007.     $attributes .= ' class="menu-link ' . ( $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ) . '"';
  1008.  
  1009.     //----------------------------
  1010.     $item_output = sprintf( '%1$s <a %2$s > %3$s %4$s %5$s </a> %6$s',
  1011.  
  1012.         $args->before,
  1013.         $attributes,
  1014.  
  1015.         $args->link_before,
  1016.         apply_filters( 'the_title', $item->title, $item->ID ),
  1017.         $args->link_after,
  1018.  
  1019.         $args->after
  1020.     );
  1021.  
  1022.     //----------------------------
  1023.     // build html
  1024.     //----------------------------
  1025.     $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  1026. }
  1027. }
  1028. ?>
  1029.  
  1030. <?php
  1031. //-------------------------------------------------------------------
  1032. //    select menu according to whether user is logged in
  1033. //-------------------------------------------------------------------
  1034.  
  1035. if ( is_user_logged_in() ) {
  1036.  
  1037.      wp_nav_menu( array( 'theme_location' => 'logged-in-menu' ) );
  1038. } else {
  1039.      wp_nav_menu( array( 'theme_location' => 'logged-out-menu' ) );
  1040. }
  1041. ?>
  1042.  
  1043. <?php
  1044. //-------------------------------------------------------------------
  1045. //            add css class to parent menu items
  1046. //-------------------------------------------------------------------
  1047. add_filter( 'wp_nav_menu_objects',
  1048.                               'add_menu_parent_class' );
  1049.  
  1050. //-------------------------------------------
  1051. function add_menu_parent_class( $items ) {
  1052. //-------------------------------------------
  1053.    
  1054.     $parents = array();
  1055.     foreach ( $items as $item ) {
  1056.         if ( $item->menu_item_parent && $item->menu_item_parent > 0 ) {
  1057.             $parents[] = $item->menu_item_parent;
  1058.         }
  1059.     }
  1060.    
  1061.     foreach ( $items as $item ) {
  1062.         if ( in_array( $item->ID, $parents ) ) {
  1063.             $item->classes[] = 'menu-parent-item';
  1064.         }
  1065.     }
  1066.    
  1067.     return $items;    
  1068. }
  1069.  
  1070.  
  1071.  
  1072. //-------------------------------------------------------------------
  1073. //                    using disabled function
  1074. //-------------------------------------------------------------------
  1075. ?>
  1076. <input type=\"radio\" name=\"attachments\"
  1077.    value=\"<?php echo esc_attr( $value ); ?>\" <?php disabled( $value1, $value2, false ); ?> />
  1078.  
  1079. <?php
  1080. //-------------------------------------------------------------------
  1081. //                   using custom Walker class
  1082. //-------------------------------------------------------------------
  1083. class Walker_Simple_Example extends Walker {
  1084.  
  1085.         //--------------------------------------------------------
  1086.     // Set the properties of the element
  1087.         // which give the ID of the current item and its parent
  1088.         //--------------------------------------------------------
  1089.  
  1090.     var $db_fields = array( 'parent' => 'parent_id', 'id' => 'object_id' );
  1091.  
  1092.         //--------------------------------------------------------
  1093.     // Displays start of a level. E.g '<ul>'
  1094.     // @see Walker::start_lvl()
  1095.         //--------------------------------------------------------
  1096.     function start_lvl(&$output, $depth=0, $args=array()) {
  1097.  
  1098.         $output .= "\n<ul>\n";
  1099.     }
  1100.  
  1101.         //--------------------------------------------------------
  1102.     // Displays end of a level. E.g '</ul>'
  1103.     // @see Walker::end_lvl()
  1104.         //--------------------------------------------------------
  1105.     function end_lvl(&$output, $depth=0, $args=array()) {
  1106.  
  1107.         $output .= "</ul>\n";
  1108.     }
  1109.  
  1110.         //--------------------------------------------------------
  1111.     // Displays start of an element. E.g '<li> Item Name'
  1112.     // @see Walker::start_el()
  1113.         //--------------------------------------------------------
  1114.     function start_el(&$output, $item, $depth=0, $args=array()) {
  1115.  
  1116.         $output. = "<li>".esc_attr($item->label);
  1117.     }
  1118.  
  1119.         //--------------------------------------------------------
  1120.     // Displays end of an element. E.g '</li>'
  1121.     // @see Walker::end_el()
  1122.         //--------------------------------------------------------
  1123.     function end_el(&$output, $item, $depth=0, $args=array()) {
  1124.         $output .= "</li>\n";
  1125.     }
  1126. }
  1127. $elements=array();
  1128. echo Walker_Simple_Example::walk($elements);
  1129.  
  1130. ?>
  1131. <?php
  1132. //-------------------------------------------------------------------
  1133. //          output menu using custom menu Walker class
  1134. //           (exclude top level of menu)
  1135. //-------------------------------------------------------------------
  1136. wp_nav_menu( array(
  1137.  
  1138.              'theme_location'=>'primary',
  1139.  
  1140.              'walker' =>
  1141.                         new SH_Child_Only_Walker(),
  1142.              'depth' => 0) );  
  1143.  
  1144. //-----------------------------------------------------
  1145. class SH_Child_Only_Walker extends Walker_Nav_Menu {
  1146. //-----------------------------------------------------
  1147.  
  1148.     //--------------------------------
  1149.     // Don't start the top level
  1150.     //--------------------------------
  1151.     function start_lvl(&$output, $depth=0,
  1152.                            $args=array()) {
  1153.         //--------------------------------
  1154.         if( 0 == $depth )
  1155.             return;
  1156.         parent::start_lvl(&$output, $depth,$args);
  1157.     }
  1158.  
  1159.     //--------------------------------
  1160.     // Don't end the top level
  1161.     //--------------------------------
  1162.     function end_lvl(&$output, $depth=0,
  1163.                          $args=array()) {
  1164.         //--------------------------------
  1165.         if( 0 == $depth )
  1166.             return;
  1167.         parent::end_lvl(&$output, $depth,$args);
  1168.     }
  1169.  
  1170.     //--------------------------------
  1171.     // Don't print top-level elements
  1172.     //--------------------------------
  1173.     function start_el(
  1174.                 &$output, $item, $depth=0,
  1175.                 $args=array()) {
  1176.         //--------------------------------
  1177.         if( 0 == $depth )
  1178.             return;
  1179.  
  1180.         parent::start_el(&$output, $item, $depth, $args);
  1181.     }
  1182.  
  1183.     //--------------------------------
  1184.     function end_el(&$output, $item, $depth=0,
  1185.                         $args=array()) {
  1186.         //--------------------------------
  1187.         if( 0 == $depth )
  1188.             return;
  1189.         parent::end_el(&$output, $item, $depth, $args);
  1190.     }
  1191.  
  1192.     //--------------------------------
  1193.     // Only follow down one branch
  1194.     //--------------------------------
  1195.     function display_element( $element,
  1196.                                   &$children_elements,
  1197.                                   $max_depth,
  1198.                                   $depth=0,
  1199.                                   $args,
  1200.                                   &$output ) {
  1201.         //--------------------------------
  1202.         //------------------------------------------------
  1203.         // Check if element as a 'current element' class
  1204.         //------------------------------------------------
  1205.         $current_element_markers =
  1206.                          array(
  1207.                            'current-menu-item',
  1208.                            'current-menu-parent',
  1209.                            'current-menu-ancestor' );
  1210.  
  1211.         $current_class = array_intersect(
  1212.                                          $current_element_markers,
  1213.                                          $element->classes );
  1214.  
  1215.         //------------------------------------------------
  1216.         // If element has a 'current' class,
  1217.         // it is an ancestor of the current element
  1218.         //------------------------------------------------
  1219.  
  1220.         $ancestor_of_current = !empty($current_class);
  1221.  
  1222.         //------------------------------------------------
  1223.         // If this is a top-level link and not the current,
  1224.         // or ancestor of the current menu item - stop here.
  1225.         //------------------------------------------------
  1226.  
  1227.         if ( 0 == $depth && !$ancestor_of_current)
  1228.             return
  1229.  
  1230.         parent::display_element(
  1231.                                 $element,
  1232.                                 &$children_elements,
  1233.                                 $max_depth,
  1234.                                 $depth,
  1235.                                 $args,
  1236.                                 &$output );
  1237.     }
  1238. }
  1239.  
  1240. ?>
  1241. <?php
  1242. //-------------------------------------------------------------------
  1243. //          add custom options page and help tab for it
  1244. //-------------------------------------------------------------------
  1245.  
  1246. add_action('admin_menu',
  1247.                         'my_admin_add_page');
  1248.  
  1249. //------------------------------------
  1250. function my_admin_add_page() {
  1251. //------------------------------------
  1252.  
  1253.     global $my_admin_page;
  1254.  
  1255.     $my_admin_page = add_options_page(__('My Admin Page', 'map'), __('My Admin Page', 'map'),
  1256.                                'manage_options', 'map', 'my_admin_page');
  1257.  
  1258.     //---------------------------------------------
  1259.     // Adds my_help_tab when my_admin_page loads
  1260.     //---------------------------------------------
  1261.     add_action('load-'.$my_admin_page,
  1262.                                        'my_admin_add_help_tab');
  1263. }
  1264.  
  1265. //------------------------------------
  1266. function my_admin_add_help_tab () {
  1267. //------------------------------------
  1268.     global $my_admin_page;
  1269.     $screen = get_current_screen();
  1270.  
  1271.     /*---------------------------------------------
  1272.      * Check if current screen is My Admin Page
  1273.      * Don't add help tab if it's not
  1274.     ---------------------------------------------*/
  1275.     if ( $screen->id != $my_admin_page )
  1276.         return;
  1277.  
  1278.     //-----------------------------------------------------
  1279.     // Add my_help_tab if current screen is My Admin Page
  1280.     //-----------------------------------------------------
  1281.     $screen->add_help_tab( array(
  1282.         'id'    => 'my_help_tab',
  1283.         'title' => __('My Help Tab'),
  1284.  
  1285.         'content'   => '<p>' .
  1286.                __( 'Descriptive content that will show in My Help Tab-body goes here.' ) . '</p>',
  1287.     ) );
  1288. }
  1289. ?>
  1290.  
  1291. <?php
  1292. //-------------------------------------------------------------------
  1293. //            add help tab for all admin pages
  1294. //-------------------------------------------------------------------
  1295.  
  1296. class example_help
  1297. {
  1298.     public $tabs = array(
  1299.                 //---------------------------------------
  1300.         // The assoc key represents the ID
  1301.         // It is NOT allowed to contain spaces
  1302.                 //---------------------------------------
  1303.          'EXAMPLE' => array(
  1304.              'title'   => 'TEST ME!'
  1305.             ,'content' => 'FOO'
  1306.          )
  1307.     );
  1308.         //---------------------------------
  1309.     static public function init()
  1310.         //---------------------------------
  1311.     {
  1312.         $class = __CLASS__ ;
  1313.         new $class;
  1314.     }
  1315.         //---------------------------------
  1316.     public function __construct()
  1317.         //---------------------------------
  1318.     {
  1319.         add_action( "load-{$GLOBALS['pagenow']}",
  1320.  
  1321.                                       array( $this, 'add_tabs' ), 20 );
  1322.     }
  1323.         //---------------------------------
  1324.     public function add_tabs()
  1325.         //---------------------------------
  1326.     {
  1327.         foreach ( $this->tabs as $id => $data )
  1328.         {
  1329.             get_current_screen()->add_help_tab( array(
  1330.                  'id'       => $id,
  1331.                  'title'    => __( $data['title'], 'some_textdomain' )
  1332.  
  1333.                 //-----------------------------------------------------------
  1334.                 // Use the content only if you want to add something
  1335.                 // static on every help tab.
  1336.                 // Example: Another title inside the tab
  1337.                 //-----------------------------------------------------------
  1338.                 ,'content'  => '<p>Some stuff that stays above every help text</p>'
  1339.                 ,'callback' => array( $this,
  1340.                                                        //------------------
  1341.                                                        'prepare'
  1342.                                                        //------------------
  1343.                                                   )
  1344.             ) );
  1345.         }
  1346.     }
  1347.     //---------------------------------
  1348.     public function prepare(
  1349.                            $screen, $tab )
  1350.         //---------------------------------
  1351.         {
  1352.             printf(
  1353.              '<p>%s</p>'
  1354.             ,__(
  1355.                      $tab['callback'][0]->tabs[ $tab['id'] ]['content']
  1356.                 ,'dmb_textdomain'
  1357.              )
  1358.         );
  1359.     }
  1360. }
  1361. //--------------------------------------------------------------------------
  1362. // Always add help tabs during "load-{$GLOBALS['pagenow'}".
  1363. // There're some edge cases, as for example on reading options screen, your
  1364. // Help Tabs get loaded before the built in tabs. This seems to be a core error.
  1365. //--------------------------------------------------------------------------
  1366.  
  1367. add_action( 'load-post.php', array( 'example_help', 'init' ) );
  1368. add_action( 'load-post-new.php', array( 'example_help', 'init' ) );
  1369.  
  1370.  
  1371. //-------------------------------------------------------------------
  1372. //            usage of   esc_url   and  esc_url_raw
  1373. //-------------------------------------------------------------------
  1374. ?>
  1375.  
  1376. <!-- Right -->
  1377. <?php
  1378.     $url = 'http://wordpress.org';
  1379.     $response = wp_remote_get( esc_url_raw( $url ) ); // no need to espace entities
  1380.     if ( !is_wp_error( $response ) ) {
  1381.         echo wp_remote_retrieve_body( $response );
  1382.     }
  1383. ?>
  1384.  
  1385. <!-- Wrong! Use esc_url instead! -->
  1386. <img src='<?php echo esc_url_raw( $url ); ?>' />
  1387. <a href='<?php echo esc_url_raw( $url ); ?>'>WordPress</a>
  1388.  
  1389. ?>
  1390. <?php
  1391. //-------------------------------------------------------------------
  1392. //     add images to media library and attach them to post
  1393. //-------------------------------------------------------------------
  1394.  
  1395. // $images is an array of image urls,
  1396. // $id is the ID of the post I want the images to be attached to
  1397. //---------------------------------------------------------------
  1398.  
  1399. function oo_attach_images($images, $id){
  1400.  
  1401.     require_once(ABSPATH . '/wp-admin/includes/file.php');
  1402.     require_once(ABSPATH . '/wp-admin/includes/media.php');
  1403.     require_once(ABSPATH . '/wp-admin/includes/image.php');
  1404.  
  1405.     //---------------------------------------------
  1406.     foreach($images as $image){
  1407.         //------------------------------
  1408.         //array to mimic $_FILES
  1409.         //------------------------------
  1410.         $array = array(
  1411.             //------------------------------------------------------------
  1412.             // isolates and outputs the file name from its absolute path
  1413.             //------------------------------------------------------------
  1414.             'name' => basename($image),
  1415.  
  1416.             'type' => 'image/jpeg',
  1417.             //--------------------------------------------------
  1418.             // this field passes the actual path to the image
  1419.             //--------------------------------------------------
  1420.             'tmp_name' => $image,
  1421.  
  1422.             'error' => 0,
  1423.             //--------------------------------------------------
  1424.             // returns image filesize in bytes
  1425.             //--------------------------------------------------
  1426.             'size' => filesize($image)
  1427.         );
  1428.  
  1429.         //--------------------------------------------------
  1430.         // the actual image processing, that is,
  1431.         // move to upload directory,
  1432.         // generate thumbnails and image sizes
  1433.         // and writing into the database happens here
  1434.         //--------------------------------------------------
  1435.  
  1436.         media_handle_sideload($array, $id);
  1437.     }
  1438. }
  1439. ?>
  1440.  
  1441. <?php
  1442. //-------------------------------------------------------------------
  1443. //             get file extension and mime type
  1444. //-------------------------------------------------------------------
  1445.  
  1446. $filetype = wp_check_filetype('image.jpg');
  1447.  
  1448. echo $filetype['type'];
  1449. echo $filetype['ext']; // will output jpg
  1450. ?>
  1451.  
  1452. <?php
  1453. //-------------------------------------------------------------------
  1454. //          wrong way to use wp_handle_upload
  1455. //-------------------------------------------------------------------
  1456.  
  1457. define(‘WP_DEBUG’, true);
  1458.  
  1459. $filename = "test.png";
  1460. $tmpFile = download_url("http://url.com/testing/crop/".$filename);
  1461.  
  1462. chmod($tmpFile, 0755);
  1463.  
  1464. $mimeType = wp_check_filetype($_SERVER['DOCUMENT_ROOT'] . '/testing/crop/'.$filename);
  1465.  
  1466. $file_array = array(
  1467.  
  1468.     'file'      => $_SERVER['DOCUMENT_ROOT'] . '/testing/crop/'.$filename,
  1469.     'url'       => $_SERVER['DOCUMENT_ROOT'] . '/testing/crop/'.$filename,
  1470.     'type'      => $mimeType['type'],
  1471.     'size'      => filesize($_SERVER['DOCUMENT_ROOT'] . '/testing/crop/'.$filename),
  1472.     'name'      => $filename,
  1473.     'tmp_name'  => $tmpFile
  1474. );
  1475.  
  1476. $image = wp_handle_upload($file_array,
  1477.  
  1478.                       array('test_form' => FALSE,
  1479.                      'test_upload' => FALSE,
  1480.                      'test_type' => FALSE));
  1481.  
  1482. print_r($file_array);
  1483. print_r($image);
  1484.  
  1485. unlink($tmpFile);
  1486. ?>
  1487.  
  1488. <?php
  1489. //-------------------------------------------------------------------
  1490. //     using wp_handle_upload to add images to media library
  1491. //-------------------------------------------------------------------
  1492. if ( ! function_exists( 'wp_handle_upload' ) )
  1493.    require_once( ABSPATH . 'wp-admin/includes/file.php' );
  1494.  
  1495. $uploadedfile = $_FILES['file'];
  1496. $upload_overrides = array( 'test_form' => false );
  1497.  
  1498. //------------------------------------------------------
  1499. $movefile = wp_handle_upload(
  1500.                    $uploadedfile, $upload_overrides );
  1501. //------------------------------------------------------
  1502. if ( $movefile ) {
  1503.  
  1504.     echo "File is valid, and was successfully uploaded.\n";
  1505.     var_dump( $movefile);
  1506.  
  1507. } else {
  1508.  
  1509.     echo "Possible file upload attack!\n";
  1510. }
  1511.  
  1512. ?>
  1513. <?php
  1514. //-------------------------------------------------------------------
  1515. //       get real file ext and sanitize file name
  1516. //-------------------------------------------------------------------
  1517.  
  1518. $validate = wp_check_filetype_and_ext( $file, $filename, $mimes );
  1519.  
  1520. if( $validate['proper_filename'] !== false )
  1521.     $filename = $validate['proper_filename'];
  1522.  
  1523. ?>
  1524. <?php
  1525. //-------------------------------------------------------------------
  1526. //             get theme modifications
  1527. //-------------------------------------------------------------------
  1528. $mods = get_theme_mods();
  1529. var_dump($mods);
  1530. ?>
  1531.  
  1532. output example:
  1533. array(2) { ["header_textcolor"]=> string(3) "333"
  1534.            ["header_image"]=> string(20) "random-default-image" }
  1535.  
  1536. <?php var_dump($mods['header_textcolor']); ?>
  1537.  
  1538. output example:
  1539. string(3) "333"
  1540.  
  1541. <?php
  1542. //-------------------------------------------------------------------
  1543. //                    registering shortcodes
  1544. //-------------------------------------------------------------------
  1545. // [bartag foo="foo-value"]
  1546.  
  1547. //--------------------------------------
  1548. function bartag_func( $atts ) {
  1549. //--------------------------------------
  1550.     extract( shortcode_atts( array(
  1551.         'foo' => 'something',
  1552.         'bar' => 'something else',
  1553.     ), $atts ) );
  1554.  
  1555.     return "foo = {$foo}";
  1556. }
  1557. //--------------------------------------
  1558. add_shortcode( 'bartag',
  1559.                       'bartag_func' );
  1560.  
  1561.  
  1562. //----------------------------------------------
  1563. function caption_shortcode( $atts,
  1564.                            $content = null ) {
  1565. //----------------------------------------------
  1566.  
  1567.     return '<span class="caption">' . $content . '</span>';
  1568. }
  1569. //----------------------------------------------
  1570. function caption_shortcode( $atts,
  1571.                            $content = null ) {
  1572. //----------------------------------------------
  1573.     extract( shortcode_atts( array(
  1574.         'class' => 'caption',
  1575.     ), $atts ) );
  1576.  
  1577.     return '<span class="' . esc_attr($class) . '">' . $content . '</span>';
  1578. }
  1579. //----------------------------------------------
  1580. add_shortcode( 'caption',
  1581.                     'caption_shortcode' );
  1582.  
  1583.  
  1584.  
  1585. //----------------------------------------------
  1586. function add_shortcode( $tag, $func )
  1587. function remove_shortcode( $tag )
  1588. function remove_all_shortcodes()
  1589. function shortcode_atts( $pairs, $atts )
  1590. function do_shortcode( $content )
  1591.  
  1592. ?>
  1593.  
  1594. <?php
  1595. //-------------------------------------------------------------------
  1596. //      add shortcode and MCE button for YouTube videos
  1597. //-------------------------------------------------------------------
  1598.  
  1599. //----------------------------------------
  1600. function addYouTube($atts,
  1601.                        $content = null) {
  1602. //----------------------------------------
  1603.  
  1604.         extract(shortcode_atts(array( "id" => '' ), $atts));
  1605.  
  1606.         return
  1607.         '<p style="text-align:center"> \
  1608.        <a href="http://www.youtube.com/v/'.$id.'"> \
  1609.  
  1610.           <img src="http://img.youtube.com/vi/'.$id.'/0.jpg"
  1611.                 width="400" height="300" class="aligncenter" /> \
  1612.           <span>Watch the video</span>
  1613.  
  1614.        </a></p>';
  1615. }
  1616. //----------------------------------------
  1617. add_shortcode('youtube', 'addYouTube');
  1618.  
  1619.  
  1620. //-------------------------------------------
  1621. function add_youtube_button() {
  1622. //-------------------------------------------
  1623.    if ( ! current_user_can('edit_posts') &&
  1624.         ! current_user_can('edit_pages') )
  1625.  
  1626.      return;
  1627.  
  1628.    if ( get_user_option('rich_editing') == 'true') {
  1629.  
  1630.      add_filter('mce_external_plugins',
  1631.                           //-------------------------------
  1632.                           'add_youtube_tinymce_plugin'
  1633.                           //-------------------------------
  1634.                           );
  1635.      add_filter('mce_buttons',
  1636.                           //-------------------------------
  1637.                           'register_youtube_button');
  1638.                           //-------------------------------
  1639.    }
  1640. }
  1641.  
  1642. //----------------------------------------
  1643. add_action('init',
  1644.                    'add_youtube_button');
  1645.  
  1646. //-------------------------------------------
  1647. function register_youtube_button($buttons) {
  1648. //-------------------------------------------
  1649.    array_push($buttons, "|", "brettsyoutube");
  1650.    return $buttons;
  1651. }
  1652.  
  1653. //-------------------------------------------
  1654. function add_youtube_tinymce_plugin(
  1655.                             $plugin_array) {
  1656. //-------------------------------------------
  1657.    $plugin_array['brettsyoutube'] = get_bloginfo('template_url').'/custom/editor_plugin.js';
  1658.    return $plugin_array;
  1659. }
  1660.  
  1661. //-------------------------------------------
  1662. function my_refresh_mce($ver) {
  1663. //-------------------------------------------
  1664.   $ver += 3;
  1665.   return $ver;
  1666. }
  1667.  
  1668. //-------------------------------------------
  1669. add_filter( 'tiny_mce_version',
  1670.                        'my_refresh_mce')
  1671.  
  1672. ?>
  1673.  
  1674. //---------------------------------------------------
  1675. //                editor_plugin.js
  1676. //---------------------------------------------------
  1677.  
  1678. (function() {
  1679.     tinymce.create('tinymce.plugins.BrettsYouTube', {
  1680.  
  1681.                 //--------------------------------
  1682.         init : function(ed, url) {
  1683.                 //--------------------------------
  1684.             ed.addButton(
  1685.                              //-----------------------
  1686.                              'brettsyoutube',
  1687.                              //-----------------------
  1688.                              {
  1689.                     title : 'brettsyoutube.youtube',
  1690.  
  1691.                 image : url+'/youtube.png',
  1692.  
  1693.                 onclick : function() {
  1694.                     idPattern = /(?:(?:[^v]+)+v.)?([^&=]{11})(?=&|$)/;
  1695.  
  1696.                     var vidId = prompt("YouTube Video",
  1697.                                             "Enter the id or url for your video");
  1698.  
  1699.                     var m = idPattern.exec(vidId);
  1700.  
  1701.                     if (m != null && m != 'undefined')
  1702.  
  1703.                         ed.execCommand('mceInsertContent', false,
  1704.                                                       '[youtube id="'+m[1]+'"]');
  1705.                 }
  1706.             });
  1707.         },
  1708.  
  1709.         //--------------------------------
  1710.         createControl : function(n, cm) {
  1711.         //--------------------------------
  1712.             return null;
  1713.         },
  1714.  
  1715.         //--------------------------------
  1716.         getInfo : function() {
  1717.         //--------------------------------
  1718.             return {
  1719.                 longname : "Brett's YouTube Shortcode",
  1720.                 author : 'Brett Terpstra',
  1721.                 authorurl : 'http://brettterpstra.com/',
  1722.                 infourl : 'http://brettterpstra.com/',
  1723.                 version : "1.0"
  1724.             };
  1725.         }
  1726.     });
  1727.     tinymce.PluginManager.add('brettsyoutube', tinymce.plugins.BrettsYouTube);
  1728. })();
  1729.  
  1730. <?php
  1731. //-------------------------------------------------------------------
  1732. //                add custom button to tinyMCE
  1733. //-------------------------------------------------------------------
  1734.  
  1735. add_filter('mce_external_plugins',
  1736.                                 "tinyplugin_register");
  1737.  
  1738. add_filter('mce_buttons',
  1739.                         'tinyplugin_add_button', 0);
  1740.  
  1741.  
  1742. //--------------------------------------------
  1743. function tinyplugin_add_button($buttons)
  1744. //--------------------------------------------
  1745. {
  1746.     array_push($buttons, "separator", "tinyplugin");
  1747.     return $buttons;
  1748. }
  1749.  
  1750. //--------------------------------------------
  1751. function tinyplugin_register($plugin_array)
  1752. //--------------------------------------------
  1753. {
  1754.     $url = trim(get_bloginfo('url'), "/";
  1755.     $url.= "/wp-content/plugins/tiny-plugin/editor_plugin.js";
  1756.  
  1757.     $plugin_array["tinyplugin"] = $url;
  1758.     return $plugin_array;
  1759. }
  1760.  
  1761.  
  1762.  
  1763. ?>
  1764.  
  1765. //--------------------------------------------
  1766. //           editor_plugin.js
  1767. //--------------------------------------------
  1768.  
  1769. function tiny_plugin() {
  1770.     return "[tiny-plugin]";
  1771. }
  1772. //-----------------------------
  1773. (function() {
  1774.     tinymce.create(
  1775.            'tinymce.plugins.tinyplugin',
  1776.           {
  1777.         //--------------------------
  1778.         init :
  1779.           function(ed, url){
  1780.         //--------------------------
  1781.             ed.addButton(
  1782.  
  1783.               'tinyplugin', {
  1784.  
  1785.                           title : 'Insert TinyPlugin',
  1786.  
  1787.                           onclick : function() {
  1788.                                            ed.execCommand(
  1789.                                                 'mceInsertContent',
  1790.                                                          false,
  1791.                                                        tiny_plugin()
  1792.                                                    );
  1793.                                           },
  1794.  
  1795.                           image: url + "/wand.png"
  1796.             });
  1797.         }
  1798.     });
  1799.  
  1800.     tinymce.PluginManager.add('tinyplugin', tinymce.plugins.tinyplugin);
  1801.  
  1802. })();
  1803.  
  1804. <?php
  1805. //-------------------------------------------------------------------
  1806. //                       Settings API
  1807. //-------------------------------------------------------------------
  1808.      
  1809.  
  1810.      $default_options = array (
  1811.  
  1812.          'pub_post_type' => 'post'
  1813.  
  1814.       );
  1815.  
  1816.       //---------------------------------
  1817.       add_option('dk_rss_digest_options', $default_options);
  1818.  
  1819. //-------------------------------------------------
  1820.  
  1821. add_settings_section(
  1822.                     'main_section',
  1823.                     //------------------------
  1824.  
  1825.                        'General settings',
  1826.                          //------------------------
  1827.                          'main_settings_section',
  1828.                          //------------------------
  1829.                             'dk-main-menu');
  1830.      
  1831. add_settings_field(
  1832.  
  1833.                'pub_post_type',
  1834.                 //-------------------
  1835.                   'Post type for publications',
  1836.                          //------------------------
  1837.                          'pub_post_type_setting',
  1838.                          //------------------------
  1839.                           'dk-main-menu',
  1840.  
  1841.                                    'main_section');
  1842.    
  1843.                    
  1844.                        
  1845. register_setting(
  1846.                    'dk_rss_digest_options',
  1847.                         'dk_rss_digest_options',
  1848.  
  1849.                                //------------------------
  1850.                                'validate_options'
  1851.                                //------------------------
  1852.                            );
  1853.  
  1854. //-------------------------------------------
  1855. public function main_settings_section()
  1856. //-------------------------------------------
  1857. {
  1858.    echo '<h3>'.__('DK RSS Digest Settings').'</h3>';
  1859. }
  1860.  
  1861. //-------------------------------------------
  1862. public function pub_post_type_setting()
  1863. //-------------------------------------------
  1864. {
  1865.    
  1866.       $excl = array('page','attachment','revision','nav_menu_item');
  1867.    
  1868.       if (isset($_POST['reg_post'])) {
  1869.          if ($_POST['reg_post']==1) {
  1870.             $this->dk_posttype();
  1871.          }
  1872.          else {
  1873.             array_push($excl, 'digest');
  1874.          }
  1875.       }  
  1876.       $inf = get_post_types();
  1877.    
  1878.       $inf = array_diff($inf, $excl);
  1879.      
  1880.       echo '<select id="post_type_sel_inp"
  1881.              //--------------------------------------------
  1882.              name=  "dk_rss_digest_options[pub_post_type]"  >';
  1883.               //--------------------------------------------
  1884.       foreach ($inf as $ptype) {
  1885.  
  1886.          echo '<option value="'.$ptype.
  1887.                   '" '.selected($this->_options['pub_post_type'],$ptype,false).' >'.
  1888.                    $ptype.'</option>';
  1889.       }
  1890.       echo '</select>';
  1891.      
  1892.                
  1893. }  
  1894.  
  1895. //-------------------------------------------
  1896. public function validate_options($input)
  1897. //-------------------------------------------
  1898. {
  1899.       return $input;
  1900.    }
  1901.  
  1902. //-------------------------------------------
  1903. public function dk_menu_page()
  1904. //-------------------------------------------
  1905. {
  1906.  
  1907.       $this->_w('hook triggered', __METHOD__);
  1908.      
  1909.       echo '<h3>DK RSS Digest</h3>';
  1910.  
  1911.       echo '<ul><li><a href="'.admin_url('admin.php?page=dk-rss-management').'">'.
  1912.               __('RSS management').'</a></li>';
  1913.  
  1914.       echo '<li><a href="'.admin_url('admin.php?page=dk-digest-management').'">'.
  1915.                   __('Publishing management').'</a></li>';
  1916.       echo '<li><a href="'.admin_url('admin.php?page=dk-help').'">'.__('Help').'</a></li>';
  1917.       echo '</ul>';
  1918.      
  1919.       ?>
  1920.      
  1921.          <form action="options.php" method="post">
  1922.             //-----------------------------------------------------
  1923.             <?php settings_fields( 'dk_rss_digest_options' ); ?>
  1924.             //-----------------------------------------------------
  1925.             <?php do_settings_sections('dk-main-menu'); ?>
  1926.             //-----------------------------------------------------
  1927.  
  1928.             <p class="submit">
  1929.                 <input name="Submit" type="submit" value="<?php echo __('Save Changes'); ?>" />
  1930.             </p>
  1931.             </form>
  1932.      
  1933.       <?php
  1934.    
  1935. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement