更改Wordpress中生成音频嵌入短代码的方式

问题描述 投票:2回答:2

我有一个相当恼人的问题:我有一个wordpress网站,其中包括提供音频文件。 Wordpress可以很好地过滤任何媒体插入到帖子并放入播放器,但这对于允许下载文件也不是很好。理想情况下,我想更改生成音频播放器短代码的代码,并在<a href="URL">Click here to download mp3.</a>标记之后添加[/audio],但我的PHP几乎不存在。

我目前的方法是使用python(我很清楚)编写一个每小时的cron脚本,它使用python mysql连接器直接使用mysql数据库。如果它每小时运行并查找在过去一小时内发布的包含字符串[/audio]的帖子,请拉出音频文件的URL,并使用它构建一个后续追加的链接。这种效果是相同的,在发布的帖子和链接可用之间的最大时间延迟为一小时。当我完成编码它应该工作,但我相信这不是最好的方法。

有什么建议?

php mysql wordpress audio shortcode
2个回答
1
投票

这应该这样做(将它添加到你的functions.php文件)

function wp_audio_shortcode_dowload_link( $html, $atts, $audio, $post_id, $library ) {
    $html .='<br><a href="'.$atts['src'].'">Download</a>';
    return $html;

}
add_filter( 'wp_audio_shortcode','wp_audio_shortcode_dowload_link',5,10);

1
投票

在此片段中,您有[audio]短代码的原始代码。 它位于wp-includes/media.php,从2171行开始。此代码段中的代码:

2171            function wp_audio_shortcode( $attr, $content = '' ) {
2172	        $post_id = get_post() ? get_the_ID() : 0;
2173	
2174	        static $instance = 0;
2175	        $instance++;
2176	
2177	        /**
2178	         * Filter the default audio shortcode output.
2179	         *
2180	         * If the filtered output isn't empty, it will be used instead of generating the default audio template.
2181	         *
2182	         * @since 3.6.0
2183	         *
2184	         * @param string $html     Empty variable to be replaced with shortcode markup.
2185	         * @param array  $attr     Attributes of the shortcode. @see wp_audio_shortcode()
2186	         * @param string $content  Shortcode content.
2187	         * @param int    $instance Unique numeric ID of this audio shortcode instance.
2188	         */
2189	        $override = apply_filters( 'wp_audio_shortcode_override', '', $attr, $content, $instance );
2190	        if ( '' !== $override ) {
2191	                return $override;
2192	        }
2193	
2194	        $audio = null;
2195	
2196	        $default_types = wp_get_audio_extensions();
2197	        $defaults_atts = array(
2198	                'src'      => '',
2199	                'loop'     => '',
2200	                'autoplay' => '',
2201	                'preload'  => 'none',
2202	                'class'    => 'wp-audio-shortcode',
2203	                'style'    => 'width: 100%; visibility: hidden;'
2204	        );
2205	        foreach ( $default_types as $type ) {
2206	                $defaults_atts[$type] = '';
2207	        }
2208	
2209	        $atts = shortcode_atts( $defaults_atts, $attr, 'audio' );
2210	
2211	        $primary = false;
2212	        if ( ! empty( $atts['src'] ) ) {
2213	                $type = wp_check_filetype( $atts['src'], wp_get_mime_types() );
2214	                if ( ! in_array( strtolower( $type['ext'] ), $default_types ) ) {
2215	                        return sprintf( '<a class="wp-embedded-audio" href="%s">%s</a>', esc_url( $atts['src'] ), esc_html( $atts['src'] ) );
2216	                }
2217	                $primary = true;
2218	                array_unshift( $default_types, 'src' );
2219	        } else {
2220	                foreach ( $default_types as $ext ) {
2221	                        if ( ! empty( $atts[ $ext ] ) ) {
2222	                                $type = wp_check_filetype( $atts[ $ext ], wp_get_mime_types() );
2223	                                if ( strtolower( $type['ext'] ) === $ext ) {
2224	                                        $primary = true;
2225	                                }
2226	                        }
2227	                }
2228	        }
2229	
2230	        if ( ! $primary ) {
2231	                $audios = get_attached_media( 'audio', $post_id );
2232	                if ( empty( $audios ) ) {
2233	                        return;
2234	                }
2235	
2236	                $audio = reset( $audios );
2237	                $atts['src'] = wp_get_attachment_url( $audio->ID );
2238	                if ( empty( $atts['src'] ) ) {
2239	                        return;
2240	                }
2241	
2242	                array_unshift( $default_types, 'src' );
2243	        }
2244	
2245	        /**
2246	         * Filter the media library used for the audio shortcode.
2247	         *
2248	         * @since 3.6.0
2249	         *
2250	         * @param string $library Media library used for the audio shortcode.
2251	         */
2252	        $library = apply_filters( 'wp_audio_shortcode_library', 'mediaelement' );
2253	        if ( 'mediaelement' === $library && did_action( 'init' ) ) {
2254	                wp_enqueue_style( 'wp-mediaelement' );
2255	                wp_enqueue_script( 'wp-mediaelement' );
2256	        }
2257	
2258	        /**
2259	         * Filter the class attribute for the audio shortcode output container.
2260	         *
2261	         * @since 3.6.0
2262	         *
2263	         * @param string $class CSS class or list of space-separated classes.
2264	         */
2265	        $atts['class'] = apply_filters( 'wp_audio_shortcode_class', $atts['class'] );
2266	
2267	        $html_atts = array(
2268	                'class'    => $atts['class'],
2269	                'id'       => sprintf( 'audio-%d-%d', $post_id, $instance ),
2270	                'loop'     => wp_validate_boolean( $atts['loop'] ),
2271	                'autoplay' => wp_validate_boolean( $atts['autoplay'] ),
2272	                'preload'  => $atts['preload'],
2273	                'style'    => $atts['style'],
2274	        );
2275	
2276	        // These ones should just be omitted altogether if they are blank
2277	        foreach ( array( 'loop', 'autoplay', 'preload' ) as $a ) {
2278	                if ( empty( $html_atts[$a] ) ) {
2279	                        unset( $html_atts[$a] );
2280	                }
2281	        }
2282	
2283	        $attr_strings = array();
2284	        foreach ( $html_atts as $k => $v ) {
2285	                $attr_strings[] = $k . '="' . esc_attr( $v ) . '"';
2286	        }
2287	
2288	        $html = '';
2289	        if ( 'mediaelement' === $library && 1 === $instance ) {
2290	                $html .= "<!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->\n";
2291	        }
2292	        $html .= sprintf( '<audio %s controls="controls">', join( ' ', $attr_strings ) );
2293	
2294	        $fileurl = '';
2295	        $source = '<source type="%s" src="%s" />';
2296	        foreach ( $default_types as $fallback ) {
2297	                if ( ! empty( $atts[ $fallback ] ) ) {
2298	                        if ( empty( $fileurl ) ) {
2299	                                $fileurl = $atts[ $fallback ];
2300	                        }
2301	                        $type = wp_check_filetype( $atts[ $fallback ], wp_get_mime_types() );
2302	                        $url = add_query_arg( '_', $instance, $atts[ $fallback ] );
2303	                        $html .= sprintf( $source, $type['type'], esc_url( $url ) );
2304	                }
2305	        }
2306	
2307	        if ( 'mediaelement' === $library ) {
2308	                $html .= wp_mediaelement_fallback( $fileurl );
2309	        }
2310	        $html .= '</audio>';
2311	
2312	        /**
2313	         * Filter the audio shortcode output.
2314	         *
2315	         * @since 3.6.0
2316	         *
2317	         * @param string $html    Audio shortcode HTML output.
2318	         * @param array  $atts    Array of audio shortcode attributes.
2319	         * @param string $audio   Audio file.
2320	         * @param int    $post_id Post ID.
2321	         * @param string $library Media library used for the audio shortcode.
2322	         */
2323	        return apply_filters( 'wp_audio_shortcode', $html, $atts, $audio, $post_id, $library );
2324	}
2325	add_shortcode( 'audio', 'wp_audio_shortcode' );

我们的想法是创建您自己的自定义版本[音频]短代码,复制和自定义活动子主题或主题的function.php文件中的原始代码。

在注册自定义remove_shortcode( 'audio' );短代码之前,您应该首先使用[audio]

© www.soinside.com 2019 - 2024. All rights reserved.