Академический Документы
Профессиональный Документы
Культура Документы
the_ID() the_title() the_permalink() the_content() the_author() the_author_posts_link(); -returns the author url(http://localhost/wordpress/author/admin) the_date()-It will be printed once in a page the_time(get_option("date_format")) - It will be printed in all posts the_time() the_tags(',') the_category() the_post_thumbnail() comments_number('No comments','One Comment','% Comments'); -returns number of comments wp_list_comments()
Useful functions.
posts_nav_link() -navigation link to next and previous post. wp_get_archives('type=monthly')-returns monthly archive wp_get_archives('type=yearly')-returns yearly archive wp_get_archives('type=daily')-returns daily archive wp_list_pages() -Will list all pages present in the blog get_search_form() get_comments_number() previous_post_link() next_post_link() get_template_part('slug','name') Ex:get_template_part( 'loop', 'index' ); will do a PHP require() for the first file that exists among these, in this priority: wp-content/themes/twentytenchild/loop-index.php wp-content/themes/twentytenchild/loop.php
get_the_term_list( $id, $taxonomy, $before, $sep, $after ) - Helps to fetch custom tag or category Ex: get_the_term_list($post->ID,category,Category: ,, ,);-It will fetch current post categories. get_the_term_list($post->ID,post_tag,Tags: ,, ,);-It will fetch current post tags.
Simple plugin
<?php /* Plugin Name: Contact Form Version:0.1 Author:Karthik Author URI:http://techpointt.wordpress.com Plugin URI:http://techpointt.wordpress.com Description: Contact form helps user's to register for your blog. Admin can view and edit the details of user's who have registered */ function signature(){ global $current_user; return "<h2>".$current_user->display_name."</h2><br /><code><i>".$current_user>user_email."</i></code>";
} add_shortcode('sign','signature'); ?> We can access this plugin with the help of shortcode [sign].
Adding styles
add_action('admin_head','tfStylesAndScripts'); function tfStylesAndScripts(){ wp_register_style('tf-style-css',plugins_url('tf-style.css',__FILE__)); wp_enqueue_style('tf_style-css',plugins_url('tf-style.css',__FILE__)); }
Database interaction
select
Global $wpdb;
Get_var()-get single value from a table; Get_row(query,ARRAY_A)-get a row from a table in Array() format $a = $wpdb->get_results("select * from wp_options limit 0,30",ARRAY_A); //fetches all matches foreach ($a as $b){ echo "<br>".$b['option_name']; }
Insert
$wpdb->insert( table_name, array( filed=>value ), Array( %s//refers that is first inserting data is string %d for number ) ); Fetch last inserted row id: $wpdb->insert_id;
Update:
$wpdb->update( $table, $data, $where, $format = null, $where_format = null ); SQL Error: $wpdb->show_errors(); $wpdb->hide_errors();
$wp_query->found_posts();
Plugin Functions:
register_activation_hook( __FILE__, array( 'YourPluginNameInit', 'on_activate' ) ); -this hook will be called during activation of plugin register_deactivation_hook( __FILE__, array( 'YourPluginNameInit', 'on_deactivate' ) ); -this hook will be called during deactivation of plugin register_uninstall_hook( __FILE__, array( 'YourPluginNameInit', 'on_uninstall' ) ); -this hook will be called during uninstall. __FILE__- results current file path
</ul>
Conditional Tags
Tag Returns True If User is Viewing
Blog home page Administration interface Single post page Blog page Archives by category Archives by tag Archives by date Search results
Shortcode
function showTechForm($atts,$content=null){ extract(shortcode_atts(array('id'=>''),$atts)); //return $id; global $wpdb; $form = $wpdb->get_row("select * from ".$wpdb->prefix."tForm where tform_id=".$id,ARRAY_A); return json_decode(stripslashes($form['tform_form']),JSON_HEX_TAG); } add_shortcode(TechForm,showTechForm); Usage: [TechForm id=1]
Ajax
$.ajax({ type:"POST", url:formUrl, data:formData, beforeSend:function(){ $('.cf_error').html('<b style="color:#FF0000">Form is being submitted</b>'); }, success:function(data){ $('.cf_error').html('<b style="color:#FF0000">Thank you!! We have received your form</b>'); }, complete:function(){ //$('#add_details').hide(); $('#add_details').each(function(){ this.reset(); }); } });
args:
category_name -category slug cat Showposts - category id. -Fetches 5 recent posts from the category.
Login Form
<?php if (!(current_user_can('level_0'))){ ?> <h2>Login</h2>
<form action="<?php echo htmlentities(get_option('home').'/wp-login.php');?>" method="post"> <input type="text" name="log" id="log" value="<?php echo wp_specialchars(stripslashes($user_login), 1) ?>" size="20" /> <input type="password" name="pwd" id="pwd" size="20" /> <input type="submit" name="submit" value="Send" class="button" /> <p> <label for="rememberme"><input name="rememberme" id="rememberme" type="checkbox" checked="checked" value="forever" /> Remember me</label> <input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>" /> </p> </form> <a href="<?php echo get_option('home'); ?>/wp-login.php?action=lostpassword">Recover password</a> <?php } else { ?> <h2>Logout</h2> <a href="<?php echo wp_logout_url(htmlentities($_SERVER['REQUEST_URI'])); ?>">logout</a><br /> <a href="http://XXX/wp-admin/">admin</a> <?php } ?>
'showposts'=>5, 'caller_get_posts'=>1 ); $my_query = new WP_Query($args); if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post(); ?> <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3> <?php endwhile; } else { ?> <h2>No related posts found!</h2> <?php } } $post = $backup; // copy it back wp_reset_query(); // to use the original query again ?>
Pagination
//Pagination function pagination($prev = '', $next = '') { ?> <style type="text/css"> .page-numbers { font-size: 14px; width:30px; display:inline-block; border:1px solid #999999; textalign:center} .page-numbers.current { color: #222; } .page-numbers .dots { letter-spacing: 1px } a.page-numbers { font-size: 14px; color: #3888ff; } </style> <?php global $wp_query, $wp_rewrite; $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1; $pagination = array( 'base' => @add_query_arg('paged','%#%'), 'format' => '', 'total' => $wp_query->max_num_pages, 'current' => $current, 'prev_text' => __($prev), 'next_text' => __($next), 'type' => 'plain' ); if( $wp_rewrite->using_permalinks() ) $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' ); if( !empty($wp_query->query_vars['s']) ) $pagination['add_args'] = array( 's' => get_query_var( 's' ) );