欢迎光临
请一秒记住我们的网址:www.xinfangs.com !

免插件纯代码打造wordpress读者文章投稿页面

时间煮雨阅读(1478)

很多网站都想开放读者的投稿功能,接受读者的投稿,不仅可以丰富博客的内容,还可以增加与读者之间的沟通,可以说是一举多得的事情,何乐不为呢?WordPress本身并不提供投稿功能,但是WordPress拥有强大的扩展能力,我们可以自己来添加这个功能。效果如图:

一,新建文章投稿页面

1.在你目前使用的模板的根目录创建一个php文件,命名为 tougao.php,然后将你需要的 page.php 模板中的所有代码复制到 tougao.php 中 ;

修改 tougao.php 开头的注释,即 /* 与 */ 内的内容,修改后如下所示:

/*
 * Template name: 文章投稿
 * Description:   A tougao page
 */

二.把下面的代码甩进 functions.php:中

//投稿
if( isset($_POST['tougao_form']) && $_POST['tougao_form'] == 'send'){
    if ( isset($_COOKIE["tougao"]) && ( time() - $_COOKIE["tougao"] ) < 120 ){
        wp_die('您投稿也太勤快了吧,先歇会儿,2分钟后再来投稿吧!');
    }
    // 表单变量初始化
    $name = trim($_POST['tougao_authorname']);
    $email = trim($_POST['tougao_authoremail']);
    $site = trim($_POST['tougao_site']);
    $title = strip_tags(trim($_POST['tougao_title']));
    $category = isset( $_POST['cat'] ) ? (int)$_POST['cat'] : 0;
    $content = $_POST['tougao_content'];
    $tags = strip_tags(trim($_POST['tougao_tags']));
    if(!empty($site)){
        $author='<a href="'.$site.'" title="'.$name.'" target="_blank" rel="nofollow noopener">'.$name.'</a>';
    }else{
        $author=$name;
    }
   $info='<h2> 感谢: '.$author.' '.'投稿</h2> '.' !' ;
    global $wpdb;
    $db="SELECT post_title FROM $wpdb->posts WHERE post_title = '$title' LIMIT 1";
    if ($wpdb->get_var($db)){
        wp_die('发现重复文章.你已经发表过了.或者存在该文章');
    }
    // 表单项数据验证
    if ($name == ''){
        wp_die('昵称必须填写,且长度不得超过20个字');
    }elseif(mb_strlen($name,'UTF-8') > 20 ){
        wp_die('你的名字怎么这么长啊,起个简单易记的吧,长度不要超过20个字哟!');
    }elseif($title == ''){
        wp_die('文章标题必须填写,长度6到25个字之间');
    }elseif(mb_strlen($title,'UTF-8') > 25 ){
        wp_die('文章标题太长了,长度不得超过50个字');
    }elseif(mb_strlen($title,'UTF-8') < 6 ){
        wp_die('文章标题太短了,长度不得少于过6个字');
    }elseif ($email ==''|| !preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $email)){
        wp_die('Email必须填写,必须符合Email格式');
    }elseif ($content == ''){
        wp_die('内容必须填写,不要太长也不要太短,300到10000个字之间');
    }elseif (mb_strlen($content,'UTF-8') >20000){
        wp_die('你也太能写了吧,写这么多,别人看着也累呀,300到10000个字之间');
    }elseif (mb_strlen($content,'UTF-8') < 50){
        wp_die('太简单了吧,才写这么点,再加点内容吧,300到10000个字之间');
    }elseif ($tags == ''){
        wp_die('不要这么懒吗,加个标签好人别人搜到你的文章,长度在2到20个字!');
    }elseif (mb_strlen($tags,'UTF-8') < 2){
        wp_die('不要这么懒吗,加个标签好人别人搜到你的文章,长度在2到20个字!');
    }elseif (mb_strlen($tags,'UTF-8') > 40){
        wp_die('标签不用太长,长度在2到40个字就可以了!');
    }elseif ($site == ''){
        wp_die('请留下贵站名称,要不怎么宣传呀,这点很重要哦!');
    }elseif ($site == ''){
        wp_die('请填写原文链接,好让其他人浏览你的网站,这是最重要的宣传方式哦!');
    }else{
        $post_content = $info.'<br />'.$content;
        $tougao = array(
            'post_title' => $title,
            'post_content' => $post_content,
            'tags_input'  =>$tags,
            'post_status' => 'pending', //publish
            'post_category' => array($category)
        );
    // 将文章插入数据库
    $status = wp_insert_post( $tougao );
    if ($status != 0){
        setcookie("tougao", time(), time()+1);
        echo ('<div style="text-align:center;">'.'<title>'.'你好!'.'</title>'.'</div>');
        echo ('<div style="text-align:center;">'.'<meta charset="UTF-8" /><meta http-equiv="refresh" content="5;URL=https://www.xinfangs.com">'.'</div>');
        echo ('<div style="position:relative;font-size:14px;margin-top:100px;text-align:center;">'.'投稿成功,感谢投稿,5秒钟后将返回网站首页!'.'</div>');
        echo ('<div style="position:relative;font-size:20px;margin-top:30px;text-align:center;">'.'<a href="/" >'.'立即返回网站首页'.'</a>'.'</div>');
        wp_mail( array('aliyuncms@hotmail.com', $email), "您的投稿主人已经收到啦!", $info, array('Content-Type: text/html; charset=UTF-8') );
        die();
    }else{
        wp_die('投稿失败!');
    }
    }
}


3.在 tougao.php 中搜索: the_content,将其替换成 代码一

代码一:

<?php the_content(); ?>
<!--S 关于表单样式,请自行调整 S-->
     <form method="post" action="<?php echo $_SERVER["REQUEST_URI"]; $current_user = 
     wp_get_current_user(); ?>" class="row">
    <div class="t1 tt col-sm-4">
    <span>
        <input class="form-control" type="text" size="40" value="<?php if ( 0 != $current_user->ID ) echo $current_user->user_login; ?>" name="tougao_authorname"; id="tougao_authorname" tabindex="1" />
    </span>
    <span>
        <label>您的昵称(不超20字)</label>
    </span>
</div>
<div class="t2 tt col-sm-4">
    <span>
     <input class="form-control" type="text" size="40" value="<?php if ( 0 != $current_user->ID ) echo $current_user->user_email; ?>" name="tougao_authoremail" id="tougao_authoremail" tabindex="2" />
    </span>
    <span>
        <label>您的邮箱:</label>
    </span>
</div>
<div class="t3 tt col-sm-4">
    <span>
        <input class="form-control" type="text" size="40" value="<?php if ( 0 != $current_user->ID ) echo $current_user->user_url; ?>" name="tougao_site" id="tougao_site" tabindex="4" />
    </span>
    <span>
        <label>贵站网址:</label>
    </span>
</div>
<div class="t4 tt col-sm-4">
    <span>
        <input class="form-control" type="text" size="40" value="" name="tougao_title" id="tougao_title" tabindex="3" />
    </span>
    <span>
        <label>文章标题(6到50字之间)</label>
    </span>
</div>
<div class="t5 tt col-sm-4">
    <span>
        <input class="form-control" type="text" size="40" value="" name="tougao_tags" id="tougao_tags" tabindex="5" />
    </span>
    <span>
        <label>文章标签(20字内英文,分开)</label>
    </span>
</div>
<div class="t6 tt col-sm-4">
    <span>
        <?php wp_dropdown_categories('show_count=0&hierarchical=1&hide_empty=0'); ?>
    </span>
    <span>
        <label>文章分类 (必选)</label>
    </span>
</div>
<div class="clear"></div>
<div id="postform">
    <textarea rows="15" cols="70"  class="form-control col-sm-12" id="tougao_content" name="tougao_content" tabindex="6" /></textarea>
  
</div>
</br>
     <div><input type="hidden" value="send" name="tougao_form" />
     <button type="submit" class="btn btn-danger">提交申请</button>
     <button type="reset" class="btn btn-outline-secondary">重新填写</button>
    (提示:字数限制:<font color="red"> 300到10000字 </font> 之间 !<font color="red"> ^o^ </font>)
        </div>
                                     
</form>

<!--E 关于表单样式,请自行调整 E-->
PS:这样的投稿编辑器不是很好用,我们再来给投稿功能添加富文本编辑器

二:安装富文本编辑器:

1、下载 KindEditor 富文本编辑器
这里我们将使用 KindEditor来作为我们的编辑器,点此下载 KindEditor。下载后解压,将文件夹重命名为 kindeditor,放到你当前主题文件夹下。

2、在 tougao.php 中搜索 /form 或者将代码一中第41行的 /form 改成:

</form>
<script charset="utf-8" src="<?php bloginfo('template_url'); ?>/kindeditor/kindeditor-all-min.js"></script>
<script charset="utf-8" src="<?php bloginfo('template_url'); ?>/kindeditor/kindeditor-all.js"></script>
<script charset="utf-8" src="<?php bloginfo('template_url'); ?>/kindeditor/lang/zh_CN.js"></script>
<script>
/* 编辑器初始化代码 start */
	var editor;
	KindEditor.ready(function(K) {
		editor = K.create('#tougao_content', {
		resizeType : 1,
		allowPreviewEmoticons : false,
		allowImageUpload : true, /* 开启图片上传功能,不需要就将true改成false */
		items : [
			'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
			'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
			'insertunorderedlist', '|','pagebreak','hr','media', 'link', 'preview', 'emoticons', 'image' ,'code']
		});
	});
/* 编辑器初始化代码 end */
</script>
<script src="<?php bloginfo('template_url'); ?>/kindeditor/prism/prism.js"></script>
<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/kindeditor/prism.css">
<script charset="utf-8" src="../kindeditor-all-min.js"></script>
<script charset="utf-8" src="../lang/zh_CN.js"></script>
<script>
	... 编辑器初始化代码
</script>

3.如果感觉 样比较复杂也可以用 Wordpress 自带的 tinymce编辑器 ,可以在 /form 后下加上如下代码:

<script type="text/javascript" src="<?php echo home_url(); ?>/wp-includes/js/tinymce/tinymce.min.js"></script>
<script type="text/javascript">
  tinymce.init({
    selector : '#tougao_content',
    menubar: false,
    //toolbar: false,
});
</script>

最后再把下面的 css 甩在 style.css中即可。

/***************投稿**************/  
.form{padding:0 20px;}
.form div.tt{float:left;width:460px;margin:10px 0;}
.form input,.t6 #cat{width:250px;height:30px;padding:0 10px;}
#submit_post{margin:20px 0;}
#submit_post input{width:150px;height:50px;}
#postform{width:100%;padding:0 15px;}
#tougao_content{
    width: 100%;
}

ps:本来整理在老刘露兜即刻的文章,感谢大老的分享

WordPress主题网站底部添加网站已运行时间代码

时间煮雨阅读(1720)

之前在网站底部有网站运行时间,有的网友来问是怎么实现的,今天就把我自己用的代码分享下,有需要的直接拿去使用即可。

我们只要将下面的代码添加到网站主题的底部代码添加的位置(一般的WordPress主题都有这个功能),添加后保存即可。如果你的主题没有就添加到 footer.php 中,自行添加即可。

效果如下:

请一秒记住我们的网址:xinfangs.com   | 
<span id="runtime_span" style="color: #24a0f0;"></span>
<script type="text/javascript">function show_runtime(){window.setTimeout("show_runtime()",1000);X=new 
Date("01/04/2020 5:22:00");
Y=new Date();T=(Y.getTime()-X.getTime());M=24*60*60*1000;
a=T/M;A=Math.floor(a);b=(a-A)*24;B=Math.floor(b);c=(b-B)*60;C=Math.floor((b-B)*60);D=Math.floor((c-C)*60);
runtime_span.innerHTML="本站已经运行: "+A+"天"+B+"小时"+C+"分"+D+"秒"}show_runtime();</script>

PS:又额外整理了几款,有需要的直接拿。

代码样式二:

请一秒记住我们的网址:xinfangs.com   | 
<span id=span_dt_dt style="color: #2F889A;"></span>
<script language=javascript>
function show_date_time(){
window.setTimeout("show_date_time()", 1000);
BirthDay=new Date("2/28/2019 10:15:45");
today=new Date();
timeold=(today.getTime()-BirthDay.getTime());
sectimeold=timeold/1000
secondsold=Math.floor(sectimeold);
msPerDay=24*60*60*1000
e_daysold=timeold/msPerDay
daysold=Math.floor(e_daysold);
e_hrsold=(e_daysold-daysold)*24;
hrsold=Math.floor(e_hrsold);
e_minsold=(e_hrsold-hrsold)*60;
minsold=Math.floor((e_hrsold-hrsold)*60);
seconds=Math.floor((e_minsold-minsold)*60);
span_dt_dt.innerHTML='<font style=color:#C40000>'+daysold+'</font> 天 <font style=color:#C40000>'+hrsold+'</font> 时 <font style=color:#C40000>'+minsold+'</font> 分 <font style=color:#C40000>'+seconds+'</font> 秒';
}
show_date_time();
</script>

代码样式三:

请一秒记住我们的网址:xinfangs.com   | 
<span id="sitetime" style="color:#096"></span>
<script>
function siteTime(){
window.setTimeout("siteTime()", 1000);
var seconds = 1000
var minutes = seconds * 60
var hours = minutes * 60
var days = hours * 24
var years = days * 365
var today = new Date()
var todayYear = today.getFullYear()
var todayMonth = today.getMonth()
var todayDate = today.getDate()
var todayHour = today.getHours()
var todayMinute = today.getMinutes()
var todaySecond = today.getSeconds()
var t1 = Date.UTC(2019,2,29,10,15,45)
var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond)
var diff = t2-t1
var diffYears = Math.floor(diff/years)
var diffDays = Math.floor((diff/days)-diffYears*365)
var diffHours =Math.floor((diff-(diffYears*365+diffDays)*days)/hours)
var diffMinutes =Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes)
var diffSeconds =Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds)
document.getElementById("sitetime").innerHTML=""+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒"
}
siteTime()
</script>

如何在wordpress主题网站使用Font Awesome字体图标

时间煮雨阅读(1503)

Font Awesome 是互联网上最受欢迎的图标集。您可以访问可以使用 css 自定义的高质量矢量图像。最重要的是,Font Awesome 提供了一个免费版本,其中包含 1,600 多个免费图标供您使用。在 WordPress 中设置和使用 Font Awesome 很容易。在本文中,我们将介绍在 WordPress 中启动和运行 Font Awesome 所需的所有步骤。

最新的4.7.0版,收录了675个图标…如果你觉得本页图标太小...这里来看看。 如果你是名设计师,并且需要在Photoshop或Illustrator等其他桌面应用中使用,请访问 Font Awesome 矢量版.
这里有两个办法可以实现,一个是直接用插件,一个是用代码。

第一、插件实现

我们可以直接搜索Font Awesome Icons插件安装激活。然后根据需要进行添加到内容、导航中都可以。

第二、代码实现:

1,首先我们要通过官方下载到 fontawesome 文件包,将 font-asesome 文件夹直接解压到你主题的根目录下

2. 打开主题的 functions.php,添加如下代码:

// 代码实现 xinfangs.com
add_action( 'wp_enqueue_scripts', 'load_fontawesome_styles' );
function load_fontawesome_styles(){
    global $wp_styles;
    wp_enqueue_style( 'font-awesome', get_template_directory_uri() . '/font-awesome/css/font-awesome.min.css' );
    wp_enqueue_style( 'font-awesome-ie7', get_template_directory_uri() . '/font-awesome/css/font-awesome-ie7.min.css' );
    $wp_styles->add_data( 'font-awesome-ie7', 'conditional', 'lte IE 7' );
}

3. 大功告成,下面就是使用了,官方推荐的方法是用i标签加class来添加,例如:

<i class="fa fa-refresh fa-spin"></i>

WordPress免费主题-Qzdy发布简约极致博客主题

时间煮雨阅读(1608)

这款主题是由【秋知德雨】博主开发的WordPress 免费主题,主题风格简约,源码多套前端模板、多套分类模板、多套内容模板,主题高强度DIY,自定义网站背景图片,可以自定义头部图片,需要的自行下载!

安装注意事项

1.404问题请检查服务器伪静态规则和wp固定链接格式,推荐“/%post_id%.html”。
2.首次使用主题必须全部重置并保存一遍主题选项才能打开首页,否则可能会报错。
3.启用主题前请禁用所有插件,以免插件冲突。
4.更新主题后请重新保存主题设置。

主题下载:

1.Gitee下载(帮忙点个star):https://gitee.com/MUCEO/qzdy
2.Github下载(帮忙点个star):https://github.com/muchenkezhan/wordpress-qzdy-themes

WordPress纯代码实现给文章关键词自动添加内链接

时间煮雨阅读(1706)

文章页面的关键词自动添加内链,有利于SEO,方便读者查看所有相关关键词的所有文章。建议最多替换3个重复的词!避免过度SEO适得其反。

修改教程:

打开 functions.php 文件,添加下列代码:

/*

*Wordpress文章关键词自动添加内链链接代码

*https://www.guoguochuan.com/

*/

//连接数量

$match_num_from = 1; //一个关键字少于多少不替换

$match_num_to = 1; //一个关键字最多替换次数

//连接到WordPress的模块

add_filter('the_content','tag_link',1);

//按长度排序

function tag_sort($a, $b){

if ( $a->name == $b->name ) return 0;

return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1;

}

//改变标签关键字

function tag_link($content){

global $match_num_from,$match_num_to;

$posttags = get_the_tags();

if ($posttags) {

usort($posttags, "tag_sort");

foreach($posttags as $tag) {

$link = get_tag_link($tag->term_id);

$keyword = $tag->name;

//连接代码

$cleankeyword = stripslashes($keyword);

$url = "<a href=\"$link\" title=\"".str_replace('%s',addcslashes($cleankeyword, '$'),__('查看所有文章关于 %s'))."\"";

$url .= 'target="_blank"';

$url .= ">".addcslashes($cleankeyword, '$')."</a>";

$limit = rand($match_num_from,$match_num_to);

//不连接的代码

$content = preg_replace( '|(<a[^>]+>)(.*)('.$ex_word.')(.*)(</a[^>]*>)|U'.$case, '$1$2%&&&&&%$4$5', $content);

$content = preg_replace( '|(<img)(.*?)('.$ex_word.')(.*?)(>)|U'.$case, '$1$2%&&&&&%$4$5', $content);

$cleankeyword = preg_quote($cleankeyword,'\'');

$regEx = '\'(?!((<.*?)|(<a.*?)))('. $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))\'s' . $case;

$content = preg_replace($regEx,$url,$content,$limit);

$content = str_replace( '%&&&&&%', stripslashes($ex_word), $content);

}

}

return $content;

}

Wordpress主题:阿里百秀XIU主题v7.7版完美破解

时间煮雨阅读(1793)

XIU主题是一款Wordpress主题,支持百度熊掌号,适用于图片展示、多元化图片新闻展示、个人博客、资源分享站,扁平化设计、简洁风、全面SEO优化、多重列表展示方式 ,响应式布局支持电脑、平板和手机的完美展示。一款清晰风格的主题网站模板源码素材,主打生活资讯服务,支持自动采集功能,内置了引流脚本,可以让你的网站界面吸引更多用户。

本次XIU主题更新重点:

是几项功能新增,尤其是标签页的全面SEO功能,包含了标题、关键词和描述的设置。

值得一提的更新:新增手机端底部菜单

XIU主题7.7版本更新内容:

全面兼容 PHP 8.0

新增标签页的SEO设置(标题、关键词、描述)

新增手机端底部菜单

新增首页轮换图的alt属性以利于SEO

调整文章页图片弹窗在PC端也起作用

优化文章页图片弹窗点击效果

安装XIU主题:

方法1:进入网站后台(一般是:域名/wp-admin),点击左侧菜单中的外观,

然后点击主题,右侧会有“添加”按钮,点击“上传主题”并选择你要安装的主题压缩包 xiu.zip。

方法2:将下载的主题解压后得到 xiu 文件夹,使用FTP软件

上传 xiu 文件夹到服务器的 wp-content/themes 目录下。

请一秒记住我们的网址:www.xinfangs.com !

去投稿去留言