使用另一个功能添加新的“Insert Into Post”按钮

时间:2011-02-19 作者:PHearst

问题:添加自动添加short tag 从媒体库(视频)选项卡插入视频附件时:

Update:

这在“媒体库”选项卡下工作,不会中断图像插入等:

add_filter(\'media_send_to_editor\', \'my_filter_iste\', 20, 3);

function my_filter_iste($html, $id, $caption, $title, $align, $url, $size, $alt) {
    $attachment = get_post($id); //fetching attachment by $id passed through

    $mime_type = $attachment->post_mime_type; //getting the mime-type
    if (substr($mime_type, 0, 5) == \'video\') { //checking mime-type
        $src = wp_get_attachment_url( $id );
        $html = \'[video src="\'.$src.\'"]\';  
    }
    return $html; // return new $html
}
插入如下内容:[video src=“path/to/video/sample.mp4”]

如果您觉得可以通过任何方式清理代码,请向我们展示。

谢谢@wyrfel

3 个回复
最合适的回答,由SO网友:wyrfel 整理而成

有一个get_image_send_to_editor() wp admin/includes/media中的功能。运行以下内容的php:apply_filters( \'image_send_to_editor\', $html, $id, $caption, $title, $align, $url, $size, $alt );. 尝试挂接过滤器。

编辑:有关筛选器调用的帮助。。。挂接回调的调用如下所示:

add_filter(\'image_send_to_editor\', array(&$MyClassReference, \'filter_iste\'), 10, 8);
10是优先级,您可能需要调整它。。。默认值为10。您的筛选函数需要如下所示:

function filter_iste($html, $id, $caption, $title, $align, $url, $size, $alt) {
    ...
    return $html;
}
为了导出回调中需要执行的操作,我首先会将传递给屏幕的所有参数转储到屏幕上。如果您还需要其他内容,您可以始终使用传递的$id获取整个附件数据和元数据。

基本上,您现在可以尝试两种不同的方法:

检查附件的mime类型,如果是视频,则用您的短代码替换整个$html内容。您可以在媒体设置中添加一个选项,允许用户配置是否需要您这样做。如果他们这样做了,你只需要把整件事都勾起来。编辑:检查视频mime类型:

$attachment = get_post($id);
$mime_type = $attachment->post_mime_type;
if (substr($mime_type, 0, 5) == \'video\' && get_option(\'use_video_shorty_on_insert\')) {
    ...
}
您可以通过regexp replace运行$html内容,以删除短代码所包含的位。regexp的外观取决于$html内容的外观。我会选择第一个选项,它更坚实,更好的UI。我知道您可能想让用户能够更改您的短代码选项,但这是另一个主题,我相信可以调整媒体弹出窗口上的字段,在那里添加一些选项。

编辑3:完整示例:

// this seems to be an additional filter running for images only
add_filter(\'image_send_to_editor\', \'my_filter_iste\', 20, 8);

function my_filter_iste($html, $id, $caption, $title, $align, $url, $size, $alt) {
    $attachment = get_post($id); //fetching attachment by $id passed through
    $mime_type = $attachment->post_mime_type; //getting the mime-type
    if (substr($mime_type, 0, 5) == \'video\') { //checking mime-type
        //if a video one, replace $html by shortcode (assuming $url contains the attachment\'s file url)
        $html = \'[video src="\'.$url.\'"]\';
    }
    return $html; // return new $html
}
好的。。。显然是错误的过滤器。。。尝试以下操作:

// this seems to run when inserting a video via the \'From URL\' tab
add_filter(\'video_send_to_editor_url\', \'my_filter_vsteu\', 20, 3);

function my_filter_vsteu($html, $href, $title) {
    $html = \'[video src="\'.$href.\'"]\';
    return $html;
}
嗯,我们还有一个:

// this seems to run generically for any media item from the \'Upload\' tab
add_filter(\'media_send_to_editor\', \'my_filter_mste\', 20, 3);

function my_filter_mste($html, $send_id, $attachment) {
    if (substr($attachment->post_mime_type, 0, 5) == \'video\') {
        $href = wp_get_attachment_url($attachment->ID);
        $html = \'[video src="\'.$href.\'"]\';
    }
    return $html;
}

SO网友:Kam D

回答我的问题-

只需更改:

if ($uploadType == \'video\') {
收件人:

if (get_post_mime_type( $id ) == "application/pdf") {

SO网友:pmqa

关于视频mime检查,以下操作有效:

function my_filter_mste($html, $send_id, $attachment_id) {

global $wpdb;

$postDetails = $wpdb->get_row( $wpdb->prepare("SELECT post_mime_type FROM $wpdb->posts WHERE ID = %s", $send_id) );
$type = explode("/", $postDetails->post_mime_type);
$uploadType = strtolower($type[0]);

if ($uploadType == \'video\') {
$href = $attachment_id[\'url\']; 
$html = \'[stream flv=\'.$href.\' mp4=\'.$href.\' embed=false share=false width=480 height=360 autostart=false /]\';
}
return $html;
希望有帮助!

结束

相关推荐

Nested Shortcode Detection

如果您熟悉此代码<?php $pattern = get_shortcode_regex(); preg_match(\'/\'.$pattern.\'/s\', $posts[0]->post_content, $matches); if (is_array($matches) && $matches[2] == \'YOURSHORTCODE\') { //shortcode is being used }&#