求教:在WordPress后台新增页面/文章时添加按钮(链接)的Hook指引
Hey there! I’ve got you covered on adding that custom HTML button (link) to the WordPress post/page creation screen. Here are two reliable hooks you can use, depending on where you want the button to appear:
Option 1: Add Button to the Publish Sidebar (Miscellaneous Section)
The post_submitbox_misc_actions hook is perfect if you want the button near the publish/update controls—it blends seamlessly with the existing admin UI.
Code Example:
add_action('post_submitbox_misc_actions', 'add_custom_publish_sidebar_button'); function add_custom_publish_sidebar_button() { global $post; // Only show the button on NEW post/page screens (auto-draft status means it's a fresh entry) if ($post->post_status === 'auto-draft') { ?> <div class="misc-pub-section"> <a href="https://your-target-url.com" target="_blank" class="button button-primary"> Click Me! (Custom Button) </a> </div> <?php } }
Breakdown:
- Hook Usage:
post_submitbox_misc_actionsrenders your content in the sidebar area below the "Visibility" and "Publish" options. - Conditional Check:
$post->post_status === 'auto-draft'ensures the button only shows when creating a new post/page (not editing existing ones). Remove this line if you want it visible on edit screens too. - Styling: Using WordPress built-in classes (
button,button-primary) ensures the button matches the admin theme’s style.
Option 2: Add Button Below the Post/Page Title
If you prefer the button to sit right under the title field (above the content editor), use the edit_form_after_title hook.
Code Example:
add_action('edit_form_after_title', 'add_custom_button_below_title'); function add_custom_button_below_title() { global $post; if ($post->post_status === 'auto-draft') { ?> <div style="margin: 15px 0;"> <a href="https://your-target-url.com" target="_blank" class="button button-secondary"> My Custom Link Button </a> </div> <?php } }
Where to Put This Code:
- Theme Functions: Add the code to your active theme’s
functions.phpfile (note: changes here may be overwritten if you update the theme). - Custom Plugin: For a more permanent solution, create a simple custom plugin:
/* Plugin Name: Custom Edit Screen Button Description: Adds a custom button to WordPress post/page creation screens Version: 1.0 Author: Your Name License: GPL2 */ // Paste your chosen button function here (either Option 1 or 2)
Just replace https://your-target-url.com with your actual link, and tweak the button text to fit your needs!
内容的提问来源于stack exchange,提问作者daniel
相关产品推荐
相关产品推荐

