pulgin custom hook Or call function defined in function.php from plugin
in plugin:
…
add_action('after_setup_theme',function(){ if(function_exists('the_action_callback')) { do_action('the_action_hook'); } });
…
———
function.php
add_action('the_action_hook', 'the_action_callback'); function the_action_callback() { echo '<p>Hi I am from function.php</p>'; }
<?php $attachments = array( WP_CONTENT_DIR . '/uploads/file_to_attach.zip' ); $headers[] = 'From: My Name <[email protected]>'; $headers[] = "Content-type: text/html" ; wp_mail('[email protected]', 'subject', 'message', $headers, $attachments ); ?>
ref: http://codex.wordpress.org/Function_Reference/wp_mail
Create a folder in plugins directly called “custome-post” an create a file custome-post.php and add the following content in that file (Instead of creating plug in you can directly copy past this content in function.php it will work )
<?php /* Plugin Name: custome post sample Plugin URI: http://www.workassis.com Description: description Author: me Version: 1.0 Author URI: https://wiki.workassis.com */ class custome_post { function __construct() { #installation script register_activation_hook( __FILE__, array( $this, 'installSetup' ) ); #creating custom post type add_action( 'init', array( $this,'ticketTrackerPost') ); add_action( 'add_meta_boxes', array( $this, 'ticketTrackerPostMeta' ) ); add_action( 'save_post', array( $this, 'ticketTrackerPostMetaSave' ) ); -------------------------- } #custome post type function ticketTrackerPost() { register_post_type( 'tickettracker', array( 'labels' => array( 'name' => __( 'Ticket Tracker' ), 'singular_name' => __( 'Ticket Tracker' ), 'add_new_item' => __( 'Add New Ticket'), ), 'public' => true, 'has_archive' => true, #'rewrite' => array('slug' => 'tickettracker'), 'rewrite' => true, 'supports' => array('title') //, 'editor', 'author', 'custom-fields' ) ) ); } # Adds the meta box container. public function ticketTrackerPostMeta( $post_type ) { add_meta_box('ticketTrackerPostMeta', 'Ticket Details', array( $this, 'ticketTrackerPostMeta_render' ), 'tickettracker', 'advanced', 'high'); } # Render Meta Box content. public function ticketTrackerPostMeta_render( $post ) { require_once(dirname(__FILE__).'/addTicket.php'); } #Save the meta when the post is saved. public function ticketTrackerPostMetaSave( $post_id ) { require_once(dirname(__FILE__).'/saveTicket.php'); } } new custome_post();
addTicket.php
<?php wp_enqueue_script('ticketTrackerJs'); wp_enqueue_style("ticketTrackerCss"); wp_nonce_field( 'ticketTracker_', 'ticketTracker_nonce' ); $tfp=get_post_meta( $post->ID, 'tt_fields_priority', true ); <input class="datepicker" name="tt_fields_priority" type="text" value="<?php echo $tfp ?>" /> ?>
<?php // Check if our nonce is set. if ( ! isset( $_POST['ticketTracker_nonce'] ) ) { return $post_id; } $nonce = $_POST['ticketTracker_nonce']; // Verify that the nonce is valid. if ( ! wp_verify_nonce( $nonce, 'ticketTracker_' ) ) { return $post_id; } // If this is an autosave, our form has not been submitted, // so we don't want to do anything. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return $post_id; } // Check the user's permissions. if ( 'page' == $_POST['post_type'] ) { if ( ! current_user_can( 'edit_page', $post_id ) ) return $post_id; } else { if ( ! current_user_can( 'edit_post', $post_id ) ) return $post_id; } /* OK, its safe for us to save the data now. */ // updating to post meta foreach ($_REQUEST as $key =--> $val) { if (strpos($key, 'tt_fields_') !== false) { $data = sanitize_text_field($val); update_post_meta( $post_id, $key, $data ); } } ?>
pluginclass { ---------------- # add_filter('manage_tickettracker_posts_columns', array( $this, 'tt_table_head' ) ); #'bs_event_table_head'); add_action( 'manage_tickettracker_posts_custom_column', array( $this,'tt_table_content'), 10, 2 ); } function tt_table_head( $defaults ) { $defaults['priority'] = 'Priority'; $defaults['status'] = 'Status'; $defaults['assignee'] = 'Assignee'; $defaults['author'] = 'Createdby By'; return $defaults; } function tt_table_content( $column_name, $post_id ) { if ($column_name == 'priority') { echo $priority = get_post_meta( $post_id, 'tt_fields_priority', true ); #echo date( _x( 'F d, Y', 'Event date format', 'textdomain' ), strtotime( $event_date ) ); } if ($column_name == 'status') { $status = get_post_meta( $post_id, 'tt_fields_status', true ); echo $status; } if ($column_name == 'assignee') { echo get_post_meta( $post_id, 'tt_fields_assigned', true ); } } // note :- tickettracker :- custome post type name
wp_enqueue_script and wp_enqueue_style will not work in shortcode
public function shortcode($id=null) {
wp_print_scripts(‘scriptname’);
wp_print_styles ( ‘stylename’);
}
If you want to display a tag cloud of your most popular tags used on the site, then simply add the following code in custom-archive.php file:
1 |
<p><strong>Tags Cloud:</strong></p> |
2 |
<?php wp_tag_cloud(); ?> |
The wp_tag_cloud() function comes with a lot of parameters to adjust the number of tags, maximum and minimum tag sizes, etc.
If you want to display a list of all pages on your site, then simply add the following code:
1 |
<?php wp_list_pages( 'title_li=' ); ?> |
To display the list of authors on the site, simply add the following code:
1 |
<?php wp_list_authors( 'exclude_admin=0&optioncount=1' ); ?> |
If you want to display a list of your most recent posts, then add this code:
1 |
<?php wp_get_archives( 'type=postbypost&limit=10' ); ?> |
A comprehensive archives page allows your users to efficiently navigate through your old content. We hope that this article helped you create a custom archives page in WordPress. If you have any questions or suggestions, then please let us know by leaving a comment below.