WordPress自动添加视频和音频播放功能的方法和代码

2017年02月9日

原创内容,转载请注明出处: https://www.myzhenai.com.cn/post/2419.html https://www.myzhenai.com/thread-17971-1-1.html
关键字: wordpress自动播放视频 wordpress自动播放音频 wordpress自动显示图片 wordpress自动给视频地址加上播放代码 wordpress自动给音频地址加上播放代码
我的思路是在发布文章的时候自动将文章里的视频链接和音频链接还有图片链接加上相应的标签, 比如只需要输入视频地址,后台自动将这地址转换成html5的video标签,音频和图片类似. 而这些如果要在编辑器里添加相应功能的话,我们还需要写一堆代码和js代码,对于我这些不懂js的人来说更不好实现了, 可是我们可以用php在wordpress里实现.方法和代码都非常简单.
在wordpress当前主题目录下找到functions.php这个文件, 顾名思义,这是自定义函数文件. 我们将下边的代码添加到这个文件的相应的地方就可以了. 演示地址: https://jiayu.mybabya.com/post/3743.html

/**
##############################################################################################################################################
# Author: RucLinux 海南胡说 海南仙岛
# Website: www.myzhenai.com www.myzhenai.com.cn www.haikou-china.com jiayu.mybabya.com www.0898-shop.com
##############################################################################################################################################
#((http|ftp|https)://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?.(jpeg|jpg|png)
#((http|ftp|https)://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?.(ogg|mp3|ape|aac|wma|wav|m4a)
#((http|ftp|https)://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?.(mp4|3gp|mov|m4v|wmv|asf|mpg|mpeg|mkv)
*/
function Content_replac($text){
                preg_match_all('((http|ftp|https)://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?.(jpg|png|jpeg)', $text, $jp);
                foreach($jp as $jvalue){
                $text = str_replace($jvalue,"\n",$text);
                }
                preg_match_all('((http|ftp|https)://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?.(ogg|mp3|ape|aac|wma|wav|m4a)', $text, $oma);
                foreach($oma as $ovalue){
                $text = str_replace($ovalue,"\n",$text);		
                }
                preg_match_all('((http|ftp|https)://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?.(mp4|3gp|mov|m4v|wmv|asf|mpg|mpeg|mkv|f4v)', $text, $mwm);
                foreach($mwm as $mvalue){
                $text = str_replace($mvalue,"\n",$text);                
                }
	return $text;
}
/**

*/

 

将下边这行代码添加到 functions.php 文件低部

add_filter('the_content', 'Content_replac'); //正文

 
代码说明:

/**
##############################################################################################################################################
# Author: RucLinux 海南胡说 海南仙岛
# Website: www.myzhenai.com www.myzhenai.com.cn www.haikou-china.com jiayu.mybabya.com www.0898-shop.com
##############################################################################################################################################
#((http|ftp|https)://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?.(jpeg|jpg|png)
#((http|ftp|https)://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?.(ogg|mp3|ape|aac|wma|wav|m4a)
#((http|ftp|https)://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?.(mp4|3gp|mov|m4v|wmv|asf|mpg|mpeg|mkv)
*/
function Content_replac($text){ /** 新建一个自定义函数 */
                preg_match_all('((http|ftp|https)://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?.(jpg|png|jpeg)', $text, $jp);
                /** 用正则匹配文章内容里的所有图片地址并输出到一个数组 详情请参考 preg_match_all 函数 */
                foreach($jp as $jvalue){ /** 将数组键值循环输出到一个文本变量里 foreach是循环函数  */
                $text = str_replace($jvalue,"\n",$text); /** 将图片地址替换为图片连接代码 $jvalue变量是图片地址 */
                }
                preg_match_all('((http|ftp|https)://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?.(ogg|mp3|ape|aac|wma|wav|m4a)', $text, $oma);
                /** 用正则匹配文章内容里的所有音频链接地址并输出到一个数组 */
                foreach($oma as $ovalue){ /** 将数组键值循环输出到一个文件本变量 */
                $text = str_replace($ovalue,"\n",$text);	/** 将$ovalue这个文本变量的地址替换成audio播放标签代码 */	
                }
                preg_match_all('((http|ftp|https)://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?.(mp4|3gp|mov|m4v|wmv|asf|mpg|mpeg|mkv|f4v)', $text, $mwm); /** 用正则匹配文章内容里的所有视频地址并输出到一个数组 */
                foreach($mwm as $mvalue){ /** 将数组键值循环输出到一个文本变量里 */
                $text = str_replace($mvalue,"\n",$text);   /** 将$mvalue这个变量的视频地址替换成video标签播放代码 */             
                }
	return $text; /** 返回替换编辑过的文章内容 $text这个变量是文章内容存放变量 */
}
/**

*/

 


sicnature ---------------------------------------------------------------------
Your current IP address is: 18.209.209.246
Your IP address location: 美国弗吉尼亚阿什本
Your IP address country and region: 美国 美国
Your current browser is:
Your current system is:
Original content, please indicate the source:
同福客栈论坛 | 蟒蛇科普海南乡情论坛 | JiaYu Blog
sicnature ---------------------------------------------------------------------
Welcome to reprint. Please indicate the source http://www.myzhenai.com/post/2419.html

没有评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注