WordPress 自定义插入到帖子按钮

     2023-02-16     36

关键词:

【中文标题】WordPress 自定义插入到帖子按钮【英文标题】:WordPress Custom Insert Into Post Button 【发布时间】:2012-10-28 01:29:42 【问题描述】:

对于使用 WordPress 的媒体上传器上传的图片,有一个“插入帖子”按钮,可将短代码发送到该图片的编辑器。

我有一个文本输入,当聚焦时,我希望媒体上传器出现,以便用户可以选择图像并将文件 URL 发送到文本输入。

我遇到的主要问题是创建额外的“插入帖子”按钮,该按钮将文件 URL 发送到相应的文本字段。

我应该使用哪个钩子以及如何将文件 URL 数据返回到输入字段?

感谢您的指导!

【问题讨论】:

【参考方案1】:

你所描述的是旧的 Wordpress 做它的方式...如果你想在 Wordpress 3.5+ 中使用新的上传器,你可以创建一个 wp.media 对象来上传它,类似于 wp 中的代码-admin/js/custom-background.js:

           // Create the media frame.
            frame = wp.media.frames.customBackground = wp.media(
                // Set the title of the modal.
                title: $el.data('choose'),

                // Tell the modal to show only images.
                library: 
                    type: 'image'
                ,

                // Customize the submit button.
                button: 
                    // Set the text of the button.
                    text: $el.data('update'),
                    // Tell the button not to close the modal, since we're
                    // going to refresh the page when the image is selected.
                    close: false
                
            );

            // When an image is selected, run a callback.
            frame.on( 'select', function() 
                // Grab the selected attachment.
                var attachment = frame.state().get('selection').first();

                // Run an AJAX request to set the background image.
                $.post( ajaxurl, 
                    action: 'set-background-image',
                    attachment_id: attachment.id,
                    size: 'full'
                ).done( function() 
                    // When the request completes, reload the window.
                    window.location.reload();
                );
            );

            // Finally, open the modal.
            frame.open();

frame.on('select' function() 代码在选择文件时运行。

【讨论】:

【参考方案2】:

进一步搜索,我找到了一些很好的资源来解释如何做到这一点。我选择了 JavaScript、PHP 组合:

JavaScript

$j('input').live('focusin',function()

    var target = '#'+$j(this).attr('id');

    tb_show('','media-upload.php?post_id=[post_id]&tab=gallery&context=choose&TB_iframe=1');

    window.send_to_editor = function(html)             

        fileurl = $j(html).attr('href');
        $j(target).val(fileurl);
        tb_remove();

    ;

);

来源:http://jaspreetchahal.org/wordpress-using-media-uploader-in-your-plugin/

PHP

/* Customize button */

function media_uploader_btn($form_fields, $post) 

    $send = "<input type='submit' class='button' name='send[$post->ID]' value='" . esc_attr__( 'Choose This File' ) . "' />";

    $form_fields['buttons'] = array('tr' => "\t\t<tr class='submit'><td></td><td class='savesend'>$send</td></tr>\n");
    $form_fields['context'] = array( 'input' => 'hidden', 'value' => 'choose' );

    return $form_fields;



/* Check for button context */

function check_upload_image_context($context)

    if(isset($_REQUEST['context']) && $_REQUEST['context'] == $context)

        return true;

     elseif(isset($_POST['attachments']) && is_array($_POST['attachments']))

        /* check for context in attachment objects */

        $image_data = current($_POST['attachments']);

        if (isset($image_data['context']) && $image_data['context'] == $context )

            return true;

        

    

    return false;



if(check_upload_image_context('choose'))

    add_filter('attachment_fields_to_edit', 'media_uploader_btn', 20, 2);


来源:http://shibashake.com/wordpress-theme/how-to-hook-into-the-media-upload-popup-interface

【讨论】:

检查点击了哪个按钮的好方法。谢谢你,正是我想要的!

根据带有加载更多按钮的自定义查询获取 WordPress 帖子

】根据带有加载更多按钮的自定义查询获取WordPress帖子【英文标题】:FetchWordPressPostsaccordingtocustomquerywithloadmorebutton【发布时间】:2021-12-0922:21:17【问题描述】:我在页面上有一个2行(3-3列)的部分。所以在前5个帖子中显示,... 查看详情

在标题后立即添加按钮/链接到自定义帖子类型编辑屏幕

...】:2014-04-1106:15:56【问题描述】:我正在为客户端自定义WordPress安装的后端,并且添加了几种自定义帖子类型。它们通过一页内的自定义循环显示在前端,因此用户不应访问单个自定义帖子。为了帮助确保它们不会被意外访问,... 查看详情

自定义帖子类型重定向到 wordpress 中的主页

】自定义帖子类型重定向到wordpress中的主页【英文标题】:customposttyperedirectstohomepageinwordpress【发布时间】:2016-11-2522:01:12【问题描述】:我创建了这个自定义帖子类型functionmy_custom_post_game()$labels=array(\'name\'=>_x(\'Game\',\'posttypeg... 查看详情

将分数添加到 wordpress 自定义帖子类型 slug

】将分数添加到wordpress自定义帖子类型slug【英文标题】:Addscoretowordpresscustomposttypeslug【发布时间】:2019-09-1211:52:20【问题描述】:我的自定义帖子类型“有效”有以下代码,但当前代码不允许链接上的分数“-”?我应该解决什... 查看详情

WordPress:在自定义帖子类型中获取模块名称的帖子 slug

】WordPress:在自定义帖子类型中获取模块名称的帖子slug【英文标题】:WordPress:GetpostslugfornameofmoduleinCustomPostType【发布时间】:2021-10-0109:46:40【问题描述】:有一个名为PostModules或post-modules的自定义帖子类型。我使用高级自定义... 查看详情

将大量自定义帖子从 csv 文件导入 wordpress

】将大量自定义帖子从csv文件导入wordpress【英文标题】:Importingalargenumberofcustompostsintowordpressfromacsvfile【发布时间】:2013-10-0513:59:53【问题描述】:我的脚本存在问题,该脚本从csv文件导入条目并将它们作为wordpress自定义帖子插... 查看详情

Wordpress 自定义帖子类型分类 - 获取特定内容

】Wordpress自定义帖子类型分类-获取特定内容【英文标题】:WordpressCustomPostTypeTaxonomy-Getspecificcontent【发布时间】:2017-06-0204:44:05【问题描述】:我有一个包含食品的网站,并且我有一个名为食谱的自定义帖子类型。我需要做的是... 查看详情

php将新的自定义帖子类型添加到wordpress(代码片段)

查看详情

php将自定义帖子类型添加到rssfeed[wordpress](代码片段)

查看详情

带上传的 WordPress 3.0 自定义帖子类型

】带上传的WordPress3.0自定义帖子类型【英文标题】:WordPress3.0customposttypewithupload【发布时间】:2011-03-1601:08:24【问题描述】:有没有办法在自定义帖子类型编辑页面上插入一个(或多个)上传字段?我不想将midia库与所有字段和... 查看详情

使用 Wordpress,我如何创建一个链接到自定义帖子类型存档的新菜单项?

】使用Wordpress,我如何创建一个链接到自定义帖子类型存档的新菜单项?【英文标题】:UsingWordpress,howcanIcreateanewmenuitemthatlinkstoanarchiveofacustomposttype?【发布时间】:2017-06-0719:34:53【问题描述】:我有一个以编程方式在Wordpress中创... 查看详情

按自定义字段值订购wordpress帖子?

】按自定义字段值订购wordpress帖子?【英文标题】:Orderwordpresspostbycustomfieldvalue?【发布时间】:2011-04-0300:33:34【问题描述】:目前我已经尝试了一半的互联网代码(:D)来完成这项工作,但没有运气。我不是wordpress大师,所以有... 查看详情

从 Wordpress 取消注册自定义帖子类型

】从Wordpress取消注册自定义帖子类型【英文标题】:UnregistercustomposttypefromWordpress【发布时间】:2016-08-1311:43:27【问题描述】:我正在尝试删除通过Wordpress中的不同主题设置的自定义帖子类型,现在所有这些帖子都分配给portfolio... 查看详情

用于自定义帖子类型的 Wordpress 多个 slug

】用于自定义帖子类型的Wordpress多个slug【英文标题】:WordpressmultipleslugsforaCustomPostType【发布时间】:2015-08-1212:49:39【问题描述】:可以使用一个slug注册自定义帖子类型(例如产品)register_post_type(\'products\',$args);如何将多个slug... 查看详情

WordPress中的自定义博客存档页面

】WordPress中的自定义博客存档页面【英文标题】:CustomBlogArchivePageinWordPress【发布时间】:2014-06-1704:15:30【问题描述】:我想要做的是有一个页面列出我所有的帖子,一次十个,分页并从最近的帖子开始(第一页显示10个最近的... 查看详情

WordPress ACF:为自定义帖子类型添加默认行到管理部分的中继器字段类型

】WordPressACF:为自定义帖子类型添加默认行到管理部分的中继器字段类型【英文标题】:WordPressACF:adddefaultrowstoRepeaterfieldtypeinadminsectionforacustomposttype【发布时间】:2016-12-1010:23:23【问题描述】:WordPress的高级自定义字段插件提供... 查看详情

Wordpress 自定义帖子类型中的战略问题?

】Wordpress自定义帖子类型中的战略问题?【英文标题】:StrageProbleminWordpressCustomPostType?【发布时间】:2011-11-2118:06:32【问题描述】:我有一个博客,并且添加了一个自定义帖子类型“电影”。我已将它链接到我的single-movies.php。... 查看详情

如何在 WordPress 中显示自定义的最近帖子?

】如何在WordPress中显示自定义的最近帖子?【英文标题】:HowdoIshowcustomrecentpostsinWordPress?【发布时间】:2021-11-0307:08:15【问题描述】:现在显示如下:nowit\'sshowinglikethis我希望它看起来像这样:Iwantittolooklikethis【问题讨论】:看... 查看详情