
wp_video_shortcode ( $attr, $content = '' )
wp_video_shortcode: 这个函数用来处理WordPress中的[视频]短码。它用于在文章和页面中嵌入视频文件。
构建视频短代码输出。
这实现了视频短码的功能,用于在一个文章中显示WordPress的mp4。
function wp_video_shortcode( $attr, $content = '' ) { global $content_width; $post_id = get_post() ? get_the_ID() : 0; static $instance = 0; $instance++; /** * Filters the default video shortcode output. * * If the filtered output isn't empty, it will be used instead of generating * the default video template. * * @since 3.6.0 * * @see wp_video_shortcode() * * @param string $html Empty variable to be replaced with shortcode markup. * @param array $attr Attributes of the shortcode. @see wp_video_shortcode() * @param string $content Video shortcode content. * @param int $instance Unique numeric ID of this video shortcode instance. */ $override = apply_filters( 'wp_video_shortcode_override', '', $attr, $content, $instance ); if ( '' !== $override ) { return $override; } $video = null; $default_types = wp_get_video_extensions(); $defaults_atts = array( 'src' => '', 'poster' => '', 'loop' => '', 'autoplay' => '', 'muted' => 'false', 'preload' => 'metadata', 'width' => 640, 'height' => 360, 'class' => 'wp-video-shortcode', ); foreach ( $default_types as $type ) { $defaults_atts[ $type ] = ''; } $atts = shortcode_atts( $defaults_atts, $attr, 'video' ); if ( is_admin() ) { // Shrink the video so it isn't huge in the admin. if ( $atts['width'] > $defaults_atts['width'] ) { $atts['height'] = round( ( $atts['height'] * $defaults_atts['width'] ) / $atts['width'] ); $atts['width'] = $defaults_atts['width']; } } else { // If the video is bigger than the theme. if ( ! empty( $content_width ) && $atts['width'] > $content_width ) { $atts['height'] = round( ( $atts['height'] * $content_width ) / $atts['width'] ); $atts['width'] = $content_width; } } $is_vimeo = false; $is_youtube = false; $yt_pattern = '#^https?://(?:www.)?(?:youtube.com/watch|youtu.be/)#'; $vimeo_pattern = '#^https?://(.+.)?vimeo.com/.*#'; $primary = false; if ( ! empty( $atts['src'] ) ) { $is_vimeo = ( preg_match( $vimeo_pattern, $atts['src'] ) ); $is_youtube = ( preg_match( $yt_pattern, $atts['src'] ) ); if ( ! $is_youtube && ! $is_vimeo ) { $type = wp_check_filetype( $atts['src'], wp_get_mime_types() ); if ( ! in_array( strtolower( $type['ext'] ), $default_types, true ) ) { return sprintf( '<a class="wp-embedded-video" href="%s">%s</a>', esc_url( $atts['src'] ), esc_html( $atts['src'] ) ); } } if ( $is_vimeo ) { wp_enqueue_script( 'mediaelement-vimeo' ); } $primary = true; array_unshift( $default_types, 'src' ); } else { foreach ( $default_types as $ext ) { if ( ! empty( $atts[ $ext ] ) ) { $type = wp_check_filetype( $atts[ $ext ], wp_get_mime_types() ); if ( strtolower( $type['ext'] ) === $ext ) { $primary = true; } } } } if ( ! $primary ) { $videos = get_attached_media( 'video', $post_id ); if ( empty( $videos ) ) { return; } $video = reset( $videos ); $atts['src'] = wp_get_attachment_url( $video->ID ); if ( empty( $atts['src'] ) ) { return; } array_unshift( $default_types, 'src' ); } /** * Filters the media library used for the video shortcode. * * @since 3.6.0 * * @param string $library Media library used for the video shortcode. */ $library = apply_filters( 'wp_video_shortcode_library', 'mediaelement' ); if ( 'mediaelement' === $library && did_action( 'init' ) ) { wp_enqueue_style( 'wp-mediaelement' ); wp_enqueue_script( 'wp-mediaelement' ); wp_enqueue_script( 'mediaelement-vimeo' ); } // MediaElement.js has issues with some URL formats for Vimeo and YouTube, // so update the URL to prevent the ME.js player from breaking. if ( 'mediaelement' === $library ) { if ( $is_youtube ) { // Remove `feature` query arg and force SSL - see #40866. $atts['src'] = remove_query_arg( 'feature', $atts['src'] ); $atts['src'] = set_url_scheme( $atts['src'], 'https' ); } elseif ( $is_vimeo ) { // Remove all query arguments and force SSL - see #40866. $parsed_vimeo_url = wp_parse_url( $atts['src'] ); $vimeo_src = 'https://' . $parsed_vimeo_url['host'] . $parsed_vimeo_url['path']; // Add loop param for mejs bug - see #40977, not needed after #39686. $loop = $atts['loop'] ? '1' : '0'; $atts['src'] = add_query_arg( 'loop', $loop, $vimeo_src ); } } /** * Filters the class attribute for the video shortcode output container. * * @since 3.6.0 * @since 4.9.0 The `$atts` parameter was added. * * @param string $class CSS class or list of space-separated classes. * @param array $atts Array of video shortcode attributes. */ $atts['class'] = apply_filters( 'wp_video_shortcode_class', $atts['class'], $atts ); $html_atts = array( 'class' => $atts['class'], 'id' => sprintf( 'video-%d-%d', $post_id, $instance ), 'width' => absint( $atts['width'] ), 'height' => absint( $atts['height'] ), 'poster' => esc_url( $atts['poster'] ), 'loop' => wp_validate_boolean( $atts['loop'] ), 'autoplay' => wp_validate_boolean( $atts['autoplay'] ), 'muted' => wp_validate_boolean( $atts['muted'] ), 'preload' => $atts['preload'], ); // These ones should just be omitted altogether if they are blank. foreach ( array( 'poster', 'loop', 'autoplay', 'preload', 'muted' ) as $a ) { if ( empty( $html_atts[ $a ] ) ) { unset( $html_atts[ $a ] ); } } $attr_strings = array(); foreach ( $html_atts as $k => $v ) { $attr_strings[] = $k . '="' . esc_attr( $v ) . '"'; } $html = ''; if ( 'mediaelement' === $library && 1 === $instance ) { $html .= "<!--[if lt IE 9]><script>document.createElement('video');</script><![endif]-->n"; } $html .= sprintf( '<video %s controls="controls">', implode( ' ', $attr_strings ) ); $fileurl = ''; $source = '<source type="%s" src="%s" />'; foreach ( $default_types as $fallback ) { if ( ! empty( $atts[ $fallback ] ) ) { if ( empty( $fileurl ) ) { $fileurl = $atts[ $fallback ]; } if ( 'src' === $fallback && $is_youtube ) { $type = array( 'type' => 'video/youtube' ); } elseif ( 'src' === $fallback && $is_vimeo ) { $type = array( 'type' => 'video/vimeo' ); } else { $type = wp_check_filetype( $atts[ $fallback ], wp_get_mime_types() ); } $url = add_query_arg( '_', $instance, $atts[ $fallback ] ); $html .= sprintf( $source, $type['type'], esc_url( $url ) ); } } if ( ! empty( $content ) ) { if ( false !== strpos( $content, "n" ) ) { $content = str_replace( array( "rn", "n", "t" ), '', $content ); } $html .= trim( $content ); } if ( 'mediaelement' === $library ) { $html .= wp_mediaelement_fallback( $fileurl ); } $html .= '</video>'; $width_rule = ''; if ( ! empty( $atts['width'] ) ) { $width_rule = sprintf( 'width: %dpx;', $atts['width'] ); } $output = sprintf( '<div style="%s" class="wp-video">%s</div>', $width_rule, $html ); /** * Filters the output of the video shortcode. * * @since 3.6.0 * * @param string $output Video shortcode HTML output. * @param array $atts Array of video shortcode attributes. * @param string $video Video file. * @param int $post_id Post ID. * @param string $library Media library used for the video shortcode. */ return apply_filters( 'wp_video_shortcode', $output, $atts, $video, $post_id, $library ); }
钩子函数和过滤器函数的主要区别是它们的行为方式。钩子函数是在特定的时间点被调用的函数,它们可以执行各种任务,但通常不会返回值。而过滤器函数则是对特定的值进行过滤和修改的函数,它们会接收一个值作为输入,然后返回一个新的值作为输出。
钩子函数(Hooks)和过滤器函数(Filters)是用于修改和扩展WordPress(和其他编程环境)功能的核心概念。它们的主要区别在于它们的用途和工作方式。
1. 钩子函数(Hooks):
2. 过滤器函数(Filters):
主要区别在于它们的目的和使用场景:
重要的是要理解钩子函数和过滤器函数在编程环境中的作用,以便正确使用它们来实现自定义功能和修改数据。
apply_filters
函数用于调用一个钩子上的所有过滤器函数。它的基本用法如下:
$value = apply_filters( 'hook_name', $value, $var1, $var2, ... );
其中:
'hook_name
' 是你要应用过滤器函数的钩子名称。
$value
是你要传递给过滤器函数的值。
$var1, $var2, …
是你要传递给过滤器函数的其他参数。
常量的命名有一些约定和规则。首先,常量的名称应该全部大写,用下划线分隔单词。例如,MY_CONSTANT_NAME
。其次,常量的名称应该是有意义的,能够清晰地描述常量的用途或值。最后,避免使用已经被PHP或WordPress定义的常量名称,以避免冲突。
在WordPress中,常量的命名规则和约定如下:
ABSPATH
。WP_DEBUG
.WP_
,例如: WP_CONTENT_DIR
。例如,以下是符合WordPress常量命名规则的示例:
define( 'WP_DEBUG', true ); define( 'WP_CONTENT_DIR', '/var/www/html/wp-content' ); define( 'MY_CUSTOM_CONSTANT', 'Hello World' );
遵循这些命名规则和约定可以帮助保持代码的一致性和可读性,使得WordPress主题或插件更易于理解、维护和扩展。
要使用WordPress REST API,可以按照以下步骤进行操作:
这些是使用WordPress REST API的基本步骤。你可以根据具体需求进行深入学习和实践,探索更多可用的终点和功能。对于详细的API文档和参考,请查阅WordPress官方文档或REST API官方手册。
在WordPress中,钩子函数可以在以下几个地方使用:
add_filter()
`函数注册在特定过滤器(filter hooks)上执行的钩子函数。add_action()
`函数在特定的动作点上添加自定义内容,或使用` add_filter()
`函数修改主题函数的输出。functions.php
`的文件,用于添加自定义功能和修改WordPress行为。该文件中可以使用钩子函数来注册特定的动作点和过滤器,并添加相应的钩子函数来处理这些事件。do_action()
`或` apply_filters()
`函数调用,然后在需要的地方注册相应的钩子函数。总结起来,WordPress中的钩子函数可以在插件、主题、自定义功能文件以及WordPress核心文件中使用,用于注册和处理特定的WordPress事件或动作。这允许开发者在这些地方插入自定义逻辑,以实现个性化的功能和修改WordPress的行为。
wp_insert_post()
函数用于插入新的文章。你需要传递一个关联数组,包含文章的各项参数,如标题、内容、分类等。例如:
$post_data = array( 'post_title' => '新的文章', 'post_content' => '这是一篇新的文章。', 'post_status' => 'publish', // 设置为 'publish' 以立即发布文章 'post_type' => 'post', // 也可以设置为 'page' 或其他自定义的文章类型 ); $post_id = wp_insert_post($post_data);
如果你想要插入更复杂的文章,例如包含多个分类或标签,你可以在 $post_data
数组中添加更多的键值对。