Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- var $public_query_vars = array(
- 'm', 'p', 'posts',
- 'w', 'cat',
- 'withcomments', 'withoutcomments',
- 's', 'search', 'exact',
- 'sentence', 'calendar', 'page',
- 'paged',
- 'more',
- 'tb', 'pb',
- 'author', 'order', 'orderby',
- 'year', 'monthnum', 'day',
- 'hour','minute', 'second',
- 'name', 'category_name',
- 'tag', 'feed',
- 'author_name', 'static',
- 'pagename', 'page_id', 'error',
- 'comments_popup',
- 'attachment', 'attachment_id',
- 'subpost', 'subpost_id',
- 'preview', 'robots',
- 'taxonomy', 'term', 'cpage',
- 'post_type');
- //--------------------------------------------------------------------
- ?>
- http://dmkim1979.ru/?p=37 // single post
- http://dmkim1979.ru/?page_id=40 // single page
- http://dmkim1979.ru/?paged=4 // page 4 of 10 in archive
- http://dmkim1979.ru/?m=201310 // 10-2013 archive
- http://dmkim1979.ru/?monthnum=10 // month 10 archive
- http://dmkim1979.ru/?year=2013 // year archive
- http://dmkim1979.ru/?day=12 // daily archive (current year and month)
- http://dmkim1979.ru/?cat=3 // category archive
- http://dmkim1979.ru/?tag=tag4 // tag archive
- http://dmkim1979.ru/?rating=rating4 // custom taxonomy archive
- http://dmkim1979.ru/?post_type=tickets // custom post type archive
- http://dmkim1979.ru/?author=1 // author archive by id
- http://dmkim1979.ru/?author_name=admin // author archive by name
- http://dmkim1979.ru/?s=uuu // search archive
- http://dmkim1979.ru/?error=404 // page not found
- http://dmkim1979.ru/?feed=rss2 // output feed
- //--------------------------------------------------------------------
- 'posts', 'w', 'withcomments', 'withoutcomments', 'search', 'exact',
- 'sentence', 'calendar', 'page', 'more', 'tb', 'pb',
- 'order', 'orderby',
- 'hour','minute', 'second',
- 'name', 'category_name',
- 'static',
- 'pagename',
- 'comments_popup',
- 'attachment', 'attachment_id',
- 'subpost', 'subpost_id',
- 'preview', 'robots',
- 'taxonomy', 'term', 'cpage'
- //--------------------------------------------------------------------
- <?php
- //-------------------------------------------------------------------
- // print rewrite rules
- //-------------------------------------------------------------------
- ?>
- <div><code>
- <?php
- global $wp_rewrite;
- print_r($wp_rewrite->rules);
- ?>
- </code></div>
- <?php
- //-------------------------------------------------------------------
- // add custom rewrite rules
- //-------------------------------------------------------------------
- add_action( 'init', 'add_author_rules' );
- //----------------------------------------
- function add_author_rules() {
- //----------------------------------------
- add_rewrite_rule(
- "writer/([^/]+)/?",
- "index.php?author_name=$matches[1]",
- "top");
- add_rewrite_rule(
- "writer/([^/]+)/page/?([0-9]{1,})/?",
- "index.php?author_name=$matches[1]&paged=$matches[2]",
- "top");
- add_rewrite_rule(
- "writer/([^/]+)/(feed|rdf|rss|rss2|atom)/?",
- "index.php?author_name=$matches[1]&feed=$matches[2]",
- "top");
- add_rewrite_rule(
- "writer/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?",
- "index.php?author_name=$matches[1]&feed=$matches[2]",
- "top");
- }
- //----------------------------------------
- function generate_author_rewrite_rules() {
- //----------------------------------------
- global $wp_rewrite;
- $new_rules = array(
- "writer/([^/]+)/?" => "index.php?author_name=".$wp_rewrite->preg_index(1)
- );
- $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
- }
- //----------------------------------------
- function generate_author_rewrite_rules() {
- //----------------------------------------
- global $wp_rewrite;
- $new_rules = array(
- "writer/([^/]+)/?" => "index.php?author_name=".$wp_rewrite->preg_index(1),
- "writer/([^/]+)/page/?([0-9]{1,})/?" => "index.php?author_name=".$wp_rewrite->preg_index(1).
- "&paged=".$wp_rewrite->preg_index(2),
- "writer/([^/]+)/(feed|rdf|rss|rss2|atom)/?" => "index.php?author_name=".$wp_rewrite->preg_index(1).
- "&feed=".$wp_rewrite->preg_index(2),
- "writer/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?" => "index.php?author_name=".
- $wp_rewrite->preg_index(1)."&feed=".$wp_rewrite->preg_index(2)
- );
- $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // generate custom mod-rewrite rules
- //-------------------------------------------------------------------
- add_action('generate_rewrite_rules',
- 'themes_dir_add_rewrites');
- //----------------------------------------
- function themes_dir_add_rewrites() {
- //----------------------------------------
- $theme_name = next(explode('/themes/', get_stylesheet_directory()));
- global $wp_rewrite;
- $new_non_wp_rules = array(
- 'css/(.*)' => 'wp-content/themes/'. $theme_name . '/css/$1',
- 'js/(.*)' => 'wp-content/themes/'. $theme_name . '/js/$1',
- 'images/wordpress-urls-rewrite/(.*)' =>
- 'wp-content/themes/'. $theme_name . '/images/wordpress-urls-rewrite/$1',
- );
- $wp_rewrite->non_wp_rules += $new_non_wp_rules;
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // add custom post type to taxonomy archive
- //-------------------------------------------------------------------
- // Inside archive template:
- $taxonomies_to_match = array('the_room','the_system','the_style');
- if(is_tax($taxonomies_to_match)) {
- global $wp_query;
- query_posts(array_merge($wp_query->query_vars,array('post_type' => 'the_case_study'));
- }
- // Action Hook Method:
- //----------------------------------------
- add_action('pre_get_posts','my_post_filter');
- //----------------------------------------
- function my_post_filter($query) {
- //----------------------------------------
- $taxonomies_to_match = array('the_room','the_system','the_style');
- if(is_tax($taxonomies_to_match)) {
- $query->set('post_type','the_post_type_you_want');
- }
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // add custom rewrite rules
- //-------------------------------------------------------------------
- add_action( 'init', 'wpse_100386_rewrites' );
- //----------------------------------------
- function wpse_100386_rewrites() {
- //----------------------------------------
- add_rewrite_rule( 'case-studies/room/([^/]+)/?',
- 'index.php?the_room=$matches[1]&post_type=the_case_study', 'top' );
- add_rewrite_rule( 'videos/room/([^/]+)/?',
- 'index.php?the_room=$matches[1]&post_type=the_video', 'top' );
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // add cutom taxonomy
- //-------------------------------------------------------------------
- # -*- coding: utf-8 -*-
- /*
- Plugin Name: Custom Taxonomy Quote Author
- Plugin URI: https://gist.github.com/996608
- Description: Creates a custom taxonomy <code>Quote Author</code> with an URI <code>/qa/author-name/</code>
- Version: 1.0
- Required: 3.1
- Author: Thomas Scholz
- Author URI: http://toscho.de
- License: GPL
- */
- ! defined( 'ABSPATH' ) and exit;
- //----------------------------------------
- add_action( 'after_setup_theme', 'register_quote_author' );
- //----------------------------------------
- register_activation_hook( __FILE__, 'qua_flush' );
- register_deactivation_hook( __FILE__, 'qua_flush' );
- //----------------------------------------
- /**
- * Registers the taxonomy 'Quote Author'.
- *
- * To list the authors with links in your theme use
- * @link http://codex.wordpress.org/Function_Reference/get_the_term_list
- * <code>print get_the_term_list( get_the_ID(), 'quoteauthor' );</code>
- *
- * @link http://codex.wordpress.org/Function_Reference/register_taxonomy
- * @return void
- */
- //----------------------------------------
- function register_quote_author()
- {
- register_taxonomy(
- // Internal name
- 'quoteauthor'
- // Post types the taxonomy applies to.
- // The attachment field is not very nice, just a simple input field.
- // You may tweak that.
- , array ( 'post', 'attachment' )
- // Visible labels
- , array (
- 'labels' => array (
- 'name' => 'Quote Authors'
- , 'menu_name' => 'Quote Authors'
- , 'singular_name' => 'Quote Author'
- , 'search_items' => 'Search Quote Authors'
- , 'popular_items' => 'Popular Quote Authors'
- , 'all_items' => 'All Quote Authors'
- , 'edit_item' => 'Edit Quote Author'
- , 'update_item' => 'Update Quote Author'
- , 'add_new_item' => 'Add Quote Author'
- , 'new_item_name' => 'New name for Quote Author'
- , 'separate_items_with_commas' => 'Separate Quote Authors by comma'
- , 'add_or_remove_items' => 'Add or remove Quote Authors'
- , 'choose_from_most_used' => 'Choose from most quoted authors'
- )
- // Most important parameter. :)
- , 'public' => TRUE
- // Available in custom menus.
- , 'show_in_nav_menus' => TRUE
- // Standard box.
- , 'show_ui' => TRUE
- // Clickable list of popular authors.
- , 'show_tagcloud' => TRUE
- // URI
- , 'rewrite' => array (
- 'slug' => 'qa'
- )
- // If you want to use WP_Query.
- , 'query_var' => 'qa'
- )
- );
- }
- //----------------------------------------
- /**
- * Tells WordPress to rebuild the rewrite rules to include our custom URIS.
- *
- * @return void
- */
- //----------------------------------------
- function qua_flush()
- //----------------------------------------
- {
- // The current instance of the class WP_Rewrite.
- global $wp_rewrite;
- $wp_rewrite->flush_rules();
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // generate rewrite rules for custom post type
- //-------------------------------------------------------------------
- /**
- * Custom post type date archives
- */
- /**
- * Custom post type specific rewrite rules
- * @return wp_rewrite Rewrite rules handled by Wordpress
- */
- //----------------------------------------
- function cpt_rewrite_rules($wp_rewrite) {
- //----------------------------------------
- $rules = cpt_generate_date_archives('news', $wp_rewrite);
- $wp_rewrite->rules = $rules + $wp_rewrite->rules;
- return $wp_rewrite;
- }
- //----------------------------------------
- add_action('generate_rewrite_rules', 'cpt_rewrite_rules');
- /**
- * Generate date archive rewrite rules for a given custom post type
- * @param string $cpt slug of the custom post type
- * @return rules returns a set of rewrite rules for Wordpress to handle
- */
- //------------------------------------------------
- function cpt_generate_date_archives($cpt, $wp_rewrite) {
- //------------------------------------------------
- $rules = array();
- $post_type = get_post_type_object($cpt);
- $slug_archive = $post_type->has_archive;
- if ($slug_archive === false) return $rules;
- if ($slug_archive === true) {
- $slug_archive = $post_type->name;
- }
- $dates = array(
- array(
- 'rule' => "([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})",
- 'vars' => array('year', 'monthnum', 'day')),
- array(
- 'rule' => "([0-9]{4})/([0-9]{1,2})",
- 'vars' => array('year', 'monthnum')),
- array(
- 'rule' => "([0-9]{4})",
- 'vars' => array('year'))
- );
- foreach ($dates as $data) {
- $query = 'index.php?post_type='.$cpt;
- $rule = $slug_archive.'/'.$data['rule'];
- $i = 1;
- foreach ($data['vars'] as $var) {
- $query.= '&'.$var.'='.$wp_rewrite->preg_index($i);
- $i++;
- }
- $rules[$rule."/?$"] = $query;
- $rules[$rule."/feed/(feed|rdf|rss|rss2|atom)/?$"] = $query."&feed=".$wp_rewrite->preg_index($i);
- $rules[$rule."/(feed|rdf|rss|rss2|atom)/?$"] = $query."&feed=".$wp_rewrite->preg_index($i);
- $rules[$rule."/page/([0-9]{1,})/?$"] = $query."&paged=".$wp_rewrite->preg_index($i);
- }
- return $rules;
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // Get a montlhy archive list for a custom post type
- //-------------------------------------------------------------------
- /**
- * Get a montlhy archive list for a custom post type
- * @param string $cpt Slug of the custom post type
- * @param boolean $echo Whether to echo the output
- * @return array Return the output as an array to be parsed on the template level
- */
- //------------------------------------------------
- function get_cpt_archives( $cpt, $echo = false )
- //------------------------------------------------
- {
- global $wpdb;
- $sql = $wpdb->prepare("SELECT * FROM $wpdb->posts
- WHERE
- post_type = %s AND
- post_status = 'publish'
- GROUP BY YEAR(wp_posts.post_date),
- MONTH(wp_posts.post_date)
- ORDER BY wp_posts.post_date DESC", $cpt);
- $results = $wpdb->get_results($sql);
- if ( $results )
- {
- $archive = array();
- foreach ($results as $r)
- {
- $year = date('Y', strtotime( $r->post_date ) );
- $month = date('F', strtotime( $r->post_date ) );
- $month_num = date('m', strtotime( $r->post_date ) );
- $link = get_bloginfo('siteurl') . '/' . $cpt . '/' . $year . '/' . $month_num;
- $this_archive = array( 'month' => $month, 'year' => $year, 'link' => $link );
- array_push( $archive, $this_archive );
- }
- if( !$echo )
- return $archive;
- foreach( $archive as $a )
- {
- echo '<li><a href="' . $a['link'] . '">' . $a['month'] . ' ' . $a['year'] . '</a></li>';
- }
- }
- return false;
- }
- ?>
- To use this function just supply the slug of the custom post type, ie: get_cpt_archives( 'news' ).
- This will return an array of unique Year/Dates/Links, ie:
- Array
- (
- [0] => Array
- (
- [month] => February
- [year] => 2013
- [link] => http://yoursite.com/news/2013/02
- )
- [1] => Array
- (
- [month] => January
- [year] => 2013
- [link] => http://yoursite.com/news/2013/01
- )
- )
- <?php
- //-------------------------------------------------------------------
- // add custom rewrite rules via tag
- //-------------------------------------------------------------------
- function add_places_rewrite_tags() {
- //------------------------------------
- add_rewrite_tag('%action%','([^&]+)');
- }
- //------------------------------------
- add_action( 'init', 'add_places_rewrite_tags' );
- // First add the rewrite tag action (action=xxxx)
- //------------------------------------
- function add_places_rewrite_rules() {
- //------------------------------------
- add_rewrite_rule('^area/([^/]*)/places/?',
- 'index.php?post_type=area&name=$matches[1]&action=places','top');
- add_rewrite_rule('^area/([^/]*)/([^/]*)/places/?',
- 'index.php?post_type=area&name=$matches[2]&action=places','top');
- }
- add_action( 'init', 'add_places_rewrite_rules' );
- ?>
- <?php
- //-------------------------------------------------------------------
- // add custom variables to query
- //-------------------------------------------------------------------
- // Either directly (in your init hook):
- $wp->add_query_var( 'var1' );
- $wp->add_query_var( 'var2' );
- // Or via a filter:
- //------------------------------------
- add_filter( 'query_vars', 'wpse12965_query_vars' );
- //------------------------------------
- function wpse12965_query_vars( $query_vars )
- //------------------------------------
- {
- $query_vars[] = 'var1';
- $query_vars[] = 'var2';
- return $query_vars;
- }
- //------------------------------------
- add_action( 'init', 'wpse12065_init' );
- //------------------------------------
- function wpse12065_init()
- //------------------------------------
- {
- add_rewrite_rule(
- 'carpage(/([^/]+))?(/([^/]+))?/?',
- 'index.php?pagename=carpage&var1=$matches[2]&var2=$matches[4]',
- 'top'
- );
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // use WPEditor on front-end
- //-------------------------------------------------------------------
- /**
- * Пример вызова текстового редактора.
- */
- $settings = array(
- 'media_buttons'=>false, // Убрать кнопки загрузки мультимедийных данных.
- 'textarea_name'=>'comment', // Имя поля конечно же comment -
- // для корректной передачи информации системному обрабочику.
- 'quicktags'=>true, // Редактор со списком HTML тегов показать.
- 'tinymce'=>false, // Визуальный редактор TinyMCE скрыть.
- 'textarea_rows'=>5 // Строк в textarea пусть будет пять.
- );
- wp_editor('', 'comment', $settings);
- ?>
- <?php
- //------------------------------------
- // Пример набора кнопок для TinyMCE
- //------------------------------------
- $tinymce_settings = array(
- 'theme_advanced_buttons1'=>'link,|,bold,italic,underline,strikethrough,
- separator,blockquote,separator,bullist,numlist'
- );
- //------------------------------------
- // Пример набора кнопок для Quick-редактора.
- //------------------------------------
- $quicktags_settings = array(
- 'buttons'=>'b,i,ul,ol,li,link,close'
- );
- ?>
- <?php
- //------------------------------------
- $tinymce_settings = array(
- 'theme_advanced_buttons1' => 'link,|,bold,italic,underline,
- strikethrough,separator,blockquote,
- separator,bullist,numlist'
- );
- $settings = array(
- 'media_buttons'=>false,
- 'textarea_name'=>'comment',
- 'quicktags'=>false,.
- 'tinymce'=>$tinymce_settings, // Передаем TinyMCE произвольный набор кнопок массивом.
- 'textarea_rows'=>5
- );
- wp_editor('', 'comment', $settings);
- ?>
- <?php
- //-------------------------------------------------------------------
- // use TinyMCE in comment form
- //-------------------------------------------------------------------
- add_filter( 'comment_form_defaults', 'tinymce_comment_4265340' );
- //------------------------------------
- function tinymce_comment_4265340 ( $args ) {
- //------------------------------------
- ob_start();
- wp_editor( '', 'comment', array('tinymce') );
- $args['comment_field'] = ob_get_clean();
- return $args;
- }
- ?>
- <?php
- //-------------------------------------------------------------------
- // use TinyMCE in comment form
- //-------------------------------------------------------------------
- add_action( 'comment_form_after', 'tinyMCE_comment_form' );
- //------------------------------------
- function tinyMCE_comment_form() {
- ?>
- <script type="text/javascript" src="<?php echo includes_url( 'js/tinymce/tiny_mce.js' ); ?>">
- </script>;
- <script type="text/javascript">
- tinyMCE.init({
- theme : "advanced",
- mode: "specific_textareas",
- language: "",
- skin: "default",
- theme_advanced_buttons1: 'bold, italic, underline, blockquote,
- strikethrough, bullist, numlist,
- undo, redo, link, unlink',
- theme_advanced_buttons2: '',
- theme_advanced_buttons3: '',
- theme_advanced_buttons4: '',
- elements: 'comment',
- theme_advanced_toolbar_location : "top",
- });
- </script>
- <?php
- }
- ?>
- theme_advanced_buttons1: 'bold, italic, underline, blockquote,
- strikethrough, bullist, numlist, undo,
- redo, link, unlink',
- theme_advanced_buttons1: 'bold, italic, underline, blockquote,
- strikethrough, bullist, numlist,
- undo, redo',
- #respond table {
- margin: 0;
- padding: 0;
- }
- #respond table td {
- padding: 0;
- margin: 0;
- }
- <?php
- //-------------------------------------------------------------------
- // use TinyMCE in comment form
- //-------------------------------------------------------------------
- add_filter( 'comment_form_defaults',
- 'custom_comment_form_defaults' );
- //-------------------------------------------------
- function custom_comment_form_defaults( $args ) {
- //-------------------------------------------------
- if ( is_user_logged_in() ) {
- $mce_plugins = 'inlinepopups, fullscreen, wordpress, wplink, wpdialogs';
- } else {
- $mce_plugins = 'fullscreen, wordpress';
- }
- ob_start();
- wp_editor( '', 'comment', array(
- 'media_buttons' => true,
- 'teeny' => true,
- 'textarea_rows' => '7',
- 'tinymce' => array( 'plugins' => $mce_plugins )
- ) );
- $args['comment_field'] = ob_get_clean();
- return $args;
- }
- //-------------------------------------------------
- add_action( 'wp_enqueue_scripts',
- '__THEME_PREFIX__scripts' );
- //-------------------------------------------------
- function __THEME_PREFIX__scripts() {
- wp_enqueue_script('jquery');
- }
- //-------------------------------------------------
- add_filter( 'comment_reply_link',
- '__THEME_PREFIX__comment_reply_link' );
- //-------------------------------------------------
- function __THEME_PREFIX__comment_reply_link($link) {
- return str_replace( 'onclick=', 'data-onclick=', $link );
- }
- //-------------------------------------------------
- add_action( 'wp_head',
- '__THEME_PREFIX__wp_head' );
- //-------------------------------------------------
- function __THEME_PREFIX__wp_head() {
- //-------------------------------------------------
- ?>
- <script type="text/javascript">
- jQuery(function($){
- $('.comment-reply-link').click(function(e){
- e.preventDefault();
- var args = $(this).data('onclick');
- args = args.replace(/.*\(|\)/gi, '').replace(/\"|\s+/g, '');
- args = args.split(',');
- tinymce.EditorManager.execCommand('mceRemoveControl', true, 'comment');
- addComment.moveForm.apply( addComment, args );
- tinymce.EditorManager.execCommand('mceAddControl', true, 'comment');
- });
- });
- </script>
- <?php
- }
- //-------------------------------------------------------------------
- // tz-todoapp.php
- //-------------------------------------------------------------------
- /*
- Plugin Name: Todo App
- Plugin URI: http://tutorialzine.com
- Description: This is a demo plugin for a Tutorialzine tutorial
- Version: 1.0
- Author: Martin Angelov
- Author URI: http://tutorialzine.com
- License: GPL2
- */
- define('TZ_TODO_FILE', __FILE__);
- define('TZ_TODO_PATH', plugin_dir_path(__FILE__));
- require TZ_TODO_PATH.'includes/tzTodo.php';
- new tzTodo();
- //-------------------------------------------------------
- // includes/tzTodo.php
- //-------------------------------------------------------
- class tzTodo {
- public function __construct(){
- add_action( 'init', array($this,'init'));
- add_action('wp_ajax_nopriv_tz_ajax', array($this,'ajax'));
- add_action('wp_ajax_tz_ajax', array($this,'ajax'));
- add_filter( "manage_tz_todo_posts_columns", array($this, 'change_columns'));
- // The two last optional arguments to this function are the
- // priority (10) and number of arguments that the function expects (2):
- add_action( "manage_posts_custom_column", array($this, "custom_columns") , 10, 2 );
- }
- //-----------------------------------------
- public function init(){
- //-----------------------------------------
- if( preg_match('/\/todo\/?$/',$_SERVER['REQUEST_URI'])){
- $base_url = plugins_url( 'app/' , TZ_TODO_FILE);
- require TZ_TODO_PATH.'/app/index.php';
- exit;
- }
- $this->add_post_type();
- }
- //-----------------------------------------
- public function ajax(){
- //-----------------------------------------
- $id = -1;
- $data = '';
- $verb = '';
- $response = array();
- if(isset($_POST['verb'])){
- $verb = $_POST['verb'];
- }
- if(isset($_POST['id'])){
- $id = (int)$_POST['id'];
- }
- if(isset($_POST['data'])){
- $data = wp_strip_all_tags($_POST['data']);
- }
- $post = null;
- if($id != -1){
- $post = get_post($id);
- if($post && $post->post_type != 'tz_todo'){
- exit;
- }
- }
- switch($verb){
- //---------------------
- case 'save':
- //---------------------
- $todo_item = array(
- 'post_title' => $data,
- 'post_content' => '',
- 'post_status' => 'publish',
- 'post_type' => 'tz_todo',
- );
- if($post){
- // Adding an id to the array will cause
- // the post with that id to be edited
- // instead of a new entry to be created.
- $todo_item['ID'] = $post->ID;
- }
- $response['id'] = wp_insert_post($todo_item);
- break;
- //---------------------
- case 'check':
- //---------------------
- if($post){
- update_post_meta($post->ID, 'status', 'Completed');
- }
- break;
- //---------------------
- case 'uncheck':
- //---------------------
- if($post){
- delete_post_meta($post->ID, 'status');
- }
- break;
- //---------------------
- case 'delete':
- //---------------------
- if($post){
- wp_delete_post($post->ID);
- }
- break;
- }
- header("Content-type: application/json");
- die(json_encode($response));
- }
- //-------------------------------------------------
- private function add_post_type(){
- //-------------------------------------------------
- register_post_type( 'tz_todo',
- array(
- 'labels' => array(
- 'name' => __( 'Todo items' ),
- 'singular_name' => __( 'Todo item' )
- ),
- 'public' => true,
- 'supports' => array('title')
- )
- );
- }
- //-------------------------------------------------
- public function change_columns($cols){
- //-------------------------------------------------
- $cols = array(
- 'cb' => '<input type="checkbox" />',
- 'title' => __( 'Task' ),
- 'status' => __( 'Status' ),
- 'date' => __( 'Date' ),
- );
- return $cols;
- }
- //-------------------------------------------------
- public function custom_columns( $column, $post_id ) {
- //-------------------------------------------------
- switch ( $column ) {
- case "status":
- $status = get_post_meta( $post_id, 'status', true);
- if($status != 'Completed'){
- $status = 'Not completed';
- }
- echo $status;
- break;
- }
- }
- }
- //-------------------------------------------------------
- // /app/index.php
- //-------------------------------------------------------
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <title>Todo App Powered By WordPress | Tutorialzine Demo</title>
- <!-- This is important! It fixes the paths of the css and js files -->
- <base href="<?php echo $base_url ?>"></base>
- <!-- The stylesheets -->
- <link rel="stylesheet" href="assets/css/styles.css" />
- <script>
- // This is the URL where we need to make our AJAX calls.
- // We are making it available to JavaScript as a global variable.
- var ajaxurl = '<?php echo admin_url('admin-ajax.php')?>';
- </script>
- </head>
- <body>
- <div id="todo">
- <h2>Todo List <a href="#" class="add"
- title="Add new todo item!">✚</a></h2>
- <ul>
- <?php
- $query = new WP_Query(
- array( 'post_type'=>'tz_todo', 'order'=>'ASC')
- );
- // The Loop
- while ( $query->have_posts() ) :
- $query->the_post();
- $done = get_post_meta(get_the_id(), 'status', true) ==
- 'Completed';
- ?>
- <li data-id="<?php the_id()?>"
- class="<?php echo ($done ? 'done' : '')?>">
- <input type="checkbox"
- <?php echo ($done ? 'checked="true"' : '')?> />
- <input type="text"
- value="<?php htmlspecialchars(the_title())?>"
- placeholder="Write your todo here" />
- <a href="#" class="delete" title="Delete">✖</a>
- </li>
- <?php endwhile; ?>
- </ul>
- </div>
- <!-- JavaScript includes. -->
- <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
- <script src="assets/js/script.js"></script>
- </body>
- </html>
- //-----------------------------------------------------------------
- // /app/assets/js/script.js
- //-----------------------------------------------------------------
- $(function(){
- var saveTimer;
- var todoHolder = $('#todo');
- //--------------------------------------------
- todoHolder.on('input','li input[type=text]', function(e){
- //--------------------------------------------
- var todo = $(this),
- li = todo.closest('li');
- clearTimeout(saveTimer);
- saveTimer = setTimeout(function(){
- ajaxAction('save', li.data('id'), todo.val()).done(function(r){
- if(r.id != li.data('id')){
- li.data('id', r.id);
- }
- });
- }, 1000);
- });
- //--------------------------------------------
- todoHolder.on('change', 'li input[type=checkbox]',function(e){
- //--------------------------------------------
- var checkbox = $(this),
- li = checkbox.closest('li');
- li.toggleClass('done',checkbox.is(':checked'));
- if(checkbox.is(':checked')){
- ajaxAction('check', li.data('id'));
- }
- else{
- ajaxAction('uncheck', li.data('id'));
- }
- });
- //--------------------------------------------
- todoHolder.on('click', 'li .delete',function(e){
- //--------------------------------------------
- e.preventDefault();
- var li = $(this).closest('li');
- li.fadeOut(function(){
- li.remove();
- });
- if(li.data('id') != 0){
- ajaxAction('delete', li.data('id'));
- }
- });
- //--------------------------------------------
- todoHolder.on('click','a.add', function(e){
- //--------------------------------------------
- e.preventDefault();
- var item = $('<li data-id="0">'+
- '<input type="checkbox" />
- <input type="text" val="" placeholder="Write your todo here" />'+
- '<a href="#" class="delete">✖</a>'+
- '</li>');
- todoHolder.find('ul').append(item);
- });
- //---------------------------------------------
- function ajaxAction(verb, id, data){
- //---------------------------------------------
- return $.post(ajaxurl, {
- 'action': 'tz_ajax',
- 'verb':verb,
- 'id': id,
- 'data': data
- }, 'json');
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement