简体   繁体   中英

Wordpress: How to add buttons to post edit view

There's a million tutorials for how to customize the TinyMCE panel on the WSYWIG editor... but that's not what I want to do.

I want to add a stand-alone button to the post edit view that will make an API call and auto-fill certain fields on the page. I closest I've come so far are the hooks simple_edit_form and advanced_edit_form which both allow me to add content to the very bottom of the edit form. It'd be much better to add it at the top, where the "add media" button lives.

Is this possible?

There is an action called media buttons. Below is basic example. I've take this example from the plugin gravity forms and changed it little. Good luck.

add_action( 'media_buttons', 'add_form_button' );

function add_form_button(){
        $is_post_edit_page = in_array(RG_CURRENT_PAGE, array('post.php', 'page.php', 'page-new.php', 'post-new.php'));
        if(!$is_post_edit_page)
            return;

        // do a version check for the new 3.5 UI
        $version    = get_bloginfo('version');

        if ($version < 3.5) {
            // show button for v 3.4 and below
            $image_btn = GFCommon::get_base_url() . "/images/form-button.png";
            echo '<a href="#TB_inline?width=480&inlineId=select_gravity_form" class="thickbox" id="add_gform" title="' . __("Add Gravity Form", 'gravityforms') . '"><img src="'.$image_btn.'" alt="' . __("Add Gravity Form", 'gravityform') . '" /></a>';
        } else {
            // display button matching new UI
            echo '<style>.gform_media_icon{
                    background:url(' . GFCommon::get_base_url() . '/images/gravity-admin-icon.png) no-repeat top left;
                    display: inline-block;
                    height: 16px;
                    margin: 0 2px 0 0;
                    vertical-align: text-top;
                    width: 16px;
                    }
                    .wp-core-ui a.gform_media_link{
                     padding-left: 0.4em;
                    }
                 </style>
                  <a href="#TB_inline?width=480&inlineId=select_gravity_form" class="thickbox button gform_media_link" id="add_gform" title="' . __("Add Gravity Form", 'gravityforms') . '"><span class="gform_media_icon "></span> ' . __("Add Form", "gravityforms") . '</a>';
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM