Created
April 17, 2024 11:15
-
-
Save thoriqmacto/f29ded96ad62ef45974272d742c747bd to your computer and use it in GitHub Desktop.
Implement essential admin hooks settings features for WordPress.
This file contains 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 | |
/** | |
* Plugin Name: WP Essential Filter Hooks | |
* Description: Implement essential admin hooks settings features for WordPress. | |
* Version: 1.0 | |
* Author: macto | |
*/ | |
// Add "Duplicate" Button for Pages and Posts | |
function custom_duplicate_button($actions, $post) { | |
if ($post->post_type == 'page' || $post->post_type == 'post') { | |
$actions['duplicate'] = '<a href="' . admin_url('admin.php?action=custom_duplicate_post&post=' . $post->ID) . '">Duplicate</a>'; | |
} | |
return $actions; | |
} | |
add_filter('post_row_actions', 'custom_duplicate_button', 10, 2); | |
add_filter('page_row_actions', 'custom_duplicate_button', 10, 2); | |
function custom_duplicate_post() { | |
if (!isset($_GET['post']) || !isset($_GET['action']) || $_GET['action'] != 'custom_duplicate_post') { | |
return; | |
} | |
$post_id = $_GET['post']; | |
$post = get_post($post_id); | |
if (!isset($post) || empty($post)) { | |
return; | |
} | |
$post_content = $post->post_content; | |
$post_title = $post->post_title; | |
$post_status = $post->post_status; | |
$post_type = $post->post_type; | |
$post_author = $post->post_author; | |
$duplicate_post = array( | |
'post_title' => $post_title, | |
'post_content' => $post_content, | |
'post_status' => $post_status, | |
'post_type' => $post_type, | |
'post_author' => $post_author | |
); | |
$new_post_id = wp_insert_post($duplicate_post); | |
if ($new_post_id) { | |
wp_redirect(admin_url('post.php?post=' . $new_post_id . '&action=edit')); | |
exit; | |
} | |
} | |
add_action('admin_action_custom_duplicate_post', 'custom_duplicate_post'); | |
// Add "Order" Column Header for Pages | |
function custom_order_column($columns) { | |
// Inserting 'order' column after 'author' column | |
$new_columns = array(); | |
foreach ($columns as $key => $value) { | |
$new_columns[$key] = $value; | |
if ($key === 'author') { | |
$new_columns['order'] = 'Order'; | |
} | |
} | |
return $new_columns; | |
} | |
add_filter('manage_pages_columns', 'custom_order_column'); | |
// Display the order value in the "Order" column | |
function custom_order_column_content($column_name, $post_id) { | |
if ($column_name == 'order') { | |
// $order = get_post_meta($post_id, 'menu_order', true); | |
$order = get_post_field('menu_order', $post_id); | |
echo $order; | |
} | |
} | |
add_action('manage_pages_custom_column', 'custom_order_column_content', 10, 2); | |
// Make the "Order" column sortable | |
function custom_order_column_sortable($columns) { | |
$columns['order'] = 'menu_order'; | |
return $columns; | |
} | |
add_filter('manage_edit-page_sortable_columns', 'custom_order_column_sortable'); | |
// Modify the query to preserve hierarchical order when sorting by "Order" column | |
function custom_order_column_query($query) { | |
if (!is_admin() || !$query->is_main_query()) { | |
return; | |
} | |
$orderby = $query->get('orderby'); | |
if ($orderby === 'menu_order') { | |
$query->set('orderby', 'menu_order ID'); | |
} | |
} | |
add_action('pre_get_posts', 'custom_order_column_query'); | |
// Add hierarchical dashes to the title column when pages are sorted by menu_order | |
function custom_title_column($title, $post_id) { | |
// Check if the current request is sorting by menu_order | |
if (isset($_GET['orderby']) && $_GET['orderby'] === 'menu_order') { | |
$depth = count(get_post_ancestors($post_id)); | |
$indent = ''; | |
for ($i = 0; $i < $depth; $i++) { | |
$indent .= '— '; // Em dash used by WordPress for parent-child relationships | |
} | |
$title = $indent . $title; | |
} | |
return $title; | |
} | |
add_filter('the_title', 'custom_title_column', 10, 2); | |
// Adjust the width of the "Order" column | |
function custom_order_column_width() { | |
echo '<style type="text/css">.column-order { width: 60px; }</style>'; | |
} | |
add_action('admin_head', 'custom_order_column_width'); | |
// Enqueue the custom block variation JavaScript file | |
function custom_enqueue_block_variation_script() { | |
wp_enqueue_script( | |
'custom-block-variation-script', | |
plugins_url('custom-block-variation.js', __FILE__), | |
array('wp-blocks') | |
); | |
} | |
add_action('enqueue_block_editor_assets', 'custom_enqueue_block_variation_script'); | |
function include_feature_image_caption($block_content, $block){ | |
if ( isset($block['attrs']['className']) && $block['attrs']['className'] === 'sotp-post-featured-image') { | |
$caption = '<figcaption class="wp-element-caption" style="text-align:center;padding-top:0.5em;padding-bottom:1em;">' . get_the_post_thumbnail_caption() . '</figcaption>'; | |
$block_content = str_replace('</figure>', $caption . '</figure>', $block_content); | |
return $block_content; | |
} | |
return $block_content; | |
} | |
add_filter( 'render_block_core/post-featured-image', 'include_feature_image_caption', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment