Last active
October 13, 2017 12:43
-
-
Save vishalkakadiya/92961395b72aeb8eb2c85413347390a9 to your computer and use it in GitHub Desktop.
WordPress most useful hooks(It was used for one project, I directly copy paste it. So use it as per your requirements.)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* File contains all hooks related to Submission post type. | |
* | |
* Required in customizer.php. | |
* | |
* @package vk-research | |
*/ | |
/** | |
* Add RFPs column to submission list. | |
* | |
* @param array $columns List of columns. | |
* | |
* @return array List of columns. | |
*/ | |
function vk_research_add_rfp_column( $columns ) { | |
// Remove taxonomy column if available. | |
if ( isset( $columns['taxonomy-rfp'] ) ) { | |
unset( $columns['taxonomy-rfp'] ); | |
} | |
// Add RFPs column. | |
return array_merge( $columns, array( | |
'rfp' => __( 'RFPs', 'vk-research' ), | |
) ); | |
} | |
add_filter( 'manage_submission_posts_columns', 'vk_research_add_rfp_column' ); | |
/** | |
* Add content to RFPs column. | |
* | |
* @param string $column Name of the column. | |
* @param int $post_id ID of Post. | |
*/ | |
function vk_research_add_rfp_column_content( $column, $post_id ) { | |
if ( 'rfp' === $column ) { | |
$parent_id = get_post( $post_id )->post_parent; | |
$post_parent = get_post( $parent_id ); | |
printf( | |
'<a href="%1$s" title="%2$s">%2$s</a>', | |
esc_url( $post_parent->guid ), | |
esc_html( $post_parent->post_title ) | |
); | |
} | |
} | |
add_action( 'manage_submission_posts_custom_column', 'vk_research_add_rfp_column_content', 10, 2 ); | |
/** | |
* Make RFPs column sortable. | |
* | |
* @param array $columns List of columns. | |
* | |
* @return array List of columns. | |
*/ | |
function vk_research_rfp_sortable_column( $columns ) { | |
$columns['rfp'] = 'rfp'; | |
return $columns; | |
} | |
add_filter( 'manage_edit-submission_sortable_columns', 'vk_research_rfp_sortable_column' ); | |
/** | |
* Manage order by for RFPs column. | |
* | |
* @param string $query The query to display data. | |
*/ | |
function vk_research_rfp_orderby( $query ) { | |
if ( ! is_admin() ) { | |
return; | |
} | |
$orderby = $query->get( 'orderby' ); | |
if ( 'rfp' === $orderby ) { | |
$query->set( 'orderby', 'parent' ); | |
} | |
} | |
add_action( 'pre_get_posts', 'vk_research_rfp_orderby' ); | |
/** | |
* Add RFPs metabox for submission. | |
*/ | |
function vk_research_submission_metabox() { | |
add_meta_box( 'submission-rfps', 'RFPs', 'vk_research_submission_attributes_meta_box', 'submission', 'side', 'high' ); | |
} | |
add_action( 'add_meta_boxes', 'vk_research_submission_metabox' ); | |
/** | |
* Manage RFPs metabox content. | |
* | |
* @param object $post Post object. | |
*/ | |
function vk_research_submission_attributes_meta_box( $post ) { | |
$post_type_object = get_post_type_object( $post->post_type ); | |
if ( $post_type_object->hierarchical ) { | |
$rfps = wp_dropdown_pages( array( | |
'post_type' => 'rfp', | |
'selected' => esc_html( $post->post_parent ), | |
'name' => 'parent_id', | |
'show_option_none' => esc_html__( 'Select RFPs', 'vk-research' ), | |
'sort_column' => 'post_title', | |
'echo' => 0, | |
) ); | |
if ( ! empty( $rfps ) ) { | |
$allowed_tags = array( | |
'select' => array( | |
'name' => array(), | |
'id' => array(), | |
), | |
'option' => array( | |
'class' => array(), | |
'value' => array(), | |
'selected' => array(), | |
), | |
); | |
echo wp_kses( $rfps, $allowed_tags ); | |
} | |
} | |
} | |
/** | |
* Add RFPs dropdown for filter. | |
*/ | |
function vk_research_submission_rfp_filter() { | |
$post_type = filter_input( INPUT_GET, 'post_type', FILTER_SANITIZE_STRING ); | |
$type = ( isset( $post_type ) ) ? $post_type : 'post'; | |
// Only add filter to post type you want. | |
if ( 'submission' === $type ) { | |
$selected_rfp = filter_input( INPUT_GET, 'vk_research_rfps', FILTER_VALIDATE_INT ); | |
$rfps = wp_dropdown_pages( array( | |
'post_type' => 'rfp', | |
'selected' => esc_html( $selected_rfp ), | |
'name' => 'vk_research_rfps', | |
'show_option_none' => esc_html__( 'Select RFPs', 'vk-research' ), | |
'sort_column' => 'post_title', | |
'echo' => 0, | |
) ); | |
if ( ! empty( $rfps ) ) { | |
$allowed_tags = array( | |
'select' => array( | |
'name' => array(), | |
'id' => array(), | |
), | |
'option' => array( | |
'class' => array(), | |
'value' => array(), | |
'selected' => array(), | |
), | |
); | |
echo wp_kses( $rfps, $allowed_tags ); | |
} | |
} | |
} | |
add_action( 'restrict_manage_posts', 'vk_research_submission_rfp_filter' ); | |
/** | |
* Setup filter by post_parent. | |
* | |
* @param string $query The query to display data. | |
*/ | |
function vk_research_rfp_filter( $query ) { | |
global $pagenow; | |
$post_type = filter_input( INPUT_GET, 'post_type', FILTER_SANITIZE_STRING ); | |
$type = ( isset( $post_type ) ) ? $post_type : 'post'; | |
$selected_rfp = filter_input( INPUT_GET, 'vk_research_rfps', FILTER_VALIDATE_INT ); | |
if ( 'submission' === $type && is_admin() && 'edit.php' === $pagenow && isset( $selected_rfp ) && ! empty( $selected_rfp ) ) { | |
$query->query_vars['post_parent'] = $selected_rfp; | |
} | |
} | |
add_filter( 'pre_get_posts', 'vk_research_rfp_filter' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment