简体   繁体   English

Wordpress-在帖子编辑页面内添加表单

[英]Wordpress - Add form inside post edit page

I would like to create a new metabox in the post edit page which contains a single button which should submit a form with hidden fields. 我想在帖子编辑页面中创建一个新的metabox,其中包含一个按钮,该按钮应提交带有隐藏字段的表单。

The problem is that the post edit page is a form itself - so technically I am trying to create a form inside a form and this is not possible by HTML specification. 问题在于后期编辑页面本身就是表单-因此从技术上讲,我正在尝试在表单内部创建表单,而HTML规范无法做到这一点。

Also, I don't want to submit it via GET (simple link) because it must not get cached. 另外,我也不想通过GET(简单链接)提交,因为它不能被缓存。

This is the code: 这是代码:

function meta_box_html()
{
    ?>
    <form action="..." method="post">
        <input type="hidden" name="name" value="value">
        <input type="submit">
    </form>
    <?php
}

add_action("add_meta_boxes", function () {
    add_meta_box("id", "title", "meta_box_html", "post", "side", "high", null);
});

When I click the button - nothing happens and the page is just reloaded. 当我单击按钮时-什么也没有发生,并且页面仅被重新加载。

Any ideas how to add a form to post edit page? 有什么想法如何添加表单到发布编辑页面?

I had the same issue. 我遇到过同样的问题。 Here's how I solved it. 这是我解决的方法。

First, for convenience, I generated my form in an admin alert, as below. 首先,为方便起见,我在管理员警报中生成了表单,如下所示。 This could be dropped in an if() that checks whether the current page's ID is 'post' and parent_file is 'edit.php' if you only want this to appear on the "Edit Post" page. 如果仅希望将其显示在“编辑帖子”页面上,则可以将其放在if()中,该命令检查当前页面的ID是否为“ post”,parent_file是否为“ edit.php”。

function myform_custom_box_html($post)
{
    ?>
    <div id="myform_holder">
     <form action="<?php echo admin_url('admin-post.php'); ?>" method="post" id="myform" >
        <input type="hidden" name="action" value="myform">
        <input id="myform_fname" type="text" name="myform_fname" value="" placeholder="Recipient name" /><br />
        <input type="submit" name="myform_submit" id="rejecter_submit" class="button button-primary" value="Submit Form">
    </form>
    </div>
    <?php
}

add_action( 'admin_notices', 'myform_custom_box_html' );

Then I used a bit of js to move it to the place I wanted to in the page. 然后,我使用了一些js将其移动到页面中想要的位置。 in this case, it appends it just below the 'publish' button but you could target any element in the DOM you want. 在这种情况下,它会将其附加在“发布”按钮的正下方,但是您可以定位所需DOM中的任何元素。 You'll have to put this in a file that's properly enqueued to the wordpress admin: 您必须将其放入正确排队到wordpress admin的文件中:

$(document).ready(function() {
  var myform =  $('#myform_holder').remove();
  myform.appendTo('#major-publishing-actions');
});

Both forms work correctly with this approach. 两种形式都可以使用这种方法正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Wordpress的发布框内的编辑帖子页面中添加一个字段? - How to add a field in edit post page inside Publish box in Wordpress? 如何使WordPress的“添加/编辑帖子”形式更像Tumblr? - How to make WordPress “Add/Edit Post” form more Tumblr like? WordPress的“编辑帖子”分页符 - Wordpress “Edit Post” Page Break 如何在要回显特定页面ID的wordpress页面中添加“ edit post”功能? - How to add “edit post” function into a wordpress page in which I am echoing a specific page id? WordPress:如何添加按钮以发布编辑视图 - Wordpress: How to add buttons to post edit view 将高级自定义字段添加到 Dokan 上的现有表单 - 添加产品和编辑产品页面 (WordPress/WooCommerce) - Add Advanced Custom Field to existing form on Dokan- Add product & edit product page (WordPress/WooCommerce) 自定义帖子类型中的WordPress添加下拉列表在admin中添加/编辑帖子 - Wordpress Add dropdown in custom post type add/edit post in admin 受Wordpress保护的页面,另一页上的POST表单 - Wordpress protected page, POST form on a other page 编辑WordPress博客文章页面-Avada WordPress主题 - Edit WordPress blog post page - Avada WordPress Theme wordpress - 使用TinyMCE在后期编辑表单中管理自定义帖子元素 - wordpress - using TinyMCE for managing custom post meta in post edit form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM