为wordpress的contact form 7添加获取留言来自哪里的代码

一直以来,我都在使用coantact form 7这个插件,这个确实给我们带来了很大的方便,可是最近客户有个需求,大概就是想要知道这个消息是来自哪个页面的,因为有的客户可能会在产品单页添加一个询价的表单,然后想知道这个客户对哪个产品感兴趣。
这个确实是很常用的功能,这个时候,我们只需在contact form 7的表单的mail那里的message body添加一个代码:

[_url]

截图如下:

然后我们在前台发送一条测试消息,收到的消息,截图如下:

这样就可以让网站管理员很清楚的知道这个消息是来自哪个页面。

wordpress添加高级搜索的简单方法

我们都知道,wordpress的搜索功能并不强大,但是时至今日也没有看到wordpress在搜索上的改进,如提供分类搜索,标签搜索,自定义文章类型等这些有用的选项。因为每次搜索都要全部搜索一下,实在是没有必要。那么有没有办法实现这样的自定义的高级搜索呢?方法是有的。今天我要介绍一个更加简单的方法。那就是使用插件来实现。这款wordpress插件叫Facetious

一、Facetious插件介绍

Facetious允许我们直观的添加一个高级搜索功能,并且使用侧边栏来展示wordpress搜索框 ,其功能包括:

  • 分类 (标签, 分类目录和自定义文章分类)
  • 按月
  • 搜索输入盒

并且可以限制文章类型。

我们可以看到简单的选项,依次为小工具标题,搜索按钮文本,显示字段,文章类型设置等。

二、如何使用Facetious添加wordpress高级搜索功能

首先你需要下载Facetious,通过官方地址http://wordpress.org/plugins/facetious/或使用wordpress后台插件直接搜索安装。安装之后激活,把它放在侧边栏小工具区上即可显示wordpress高级搜索功能。这里需要说明一下:这个侧边栏小工具不一定就是在左边或右边,你完全可以把其自定义在任何一个位置。

那么如何把高级搜索功能放在wordpress主题模板呢呢?

Facetious提供了一个语法,如下:

do_action( ‘facetious’, $args );

‘$args’是数组变量:

  • submit -字节变量,提交按钮文本。
  • echo – 布尔 – 是否表单输出。
  • class – 字节 – 表单名类。
  • id – 字节 – 表单ID属性。
  • fields – 数组 – 显示字段列表。

下面是示例:

<?php 
 do_action( 'facetious', array(
 'submit' => '搜索',
 'fields' => array('s','custom_tax_1' => array(
 'label' => '选择选项',
 'class' => 'my_tax_class',
 'id' => 'my_tax_id',
 'all' => 'All terms'
 ),'custom_tax_2','m')
 )
 );
 ?>

三、Facetious前台外观展示

垂直展示:

这是默认展示。

水平展示:

这个展示需要重新定义css,你可以把下面的代码放大你的主题样式里:

#facetious_form {
background:#f4f4f4;
padding:10px;
border:1px solid #ccc;
margin-bottom:1em;
overflow:auto;
}

#facetious_form select {
width:auto;
}

#facetious_form select,
#facetious_form input {
font-size:1em;
}

#facetious_form p {
float:left;
margin:0 5px 0 0;
}

#facetious_form .facetious_submit {
float: right;
margin-top: 2em;
}

.facetious_submit_button {
background-color:#981e32;
background-repeat:repeat-x;
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr="#c32640",endColorstr="#6e1524");
background-image:-khtml-gradient(linear,left top,left bottom,from(#c32640),to(#6e1524));
background-image:-moz-linear-gradient(top,#c32640,#6e1524);
background-image:-ms-linear-gradient(top,#c32640,#6e1524);
background-image:-webkit-gradient(linear,left top,left bottom,color-stop(0%,#c32640),color-stop(100%,#6e1524));
background-image:-webkit-linear-gradient(top,#c32640,#6e1524);
background-image:-o-linear-gradient(top,#c32640,#6e1524);
background-image:linear-gradient(#c32640,#6e1524);
border-color:#6e1524 #6e1524 #981e32;
color:#fff!important;
text-shadow:0 -1px 0 rgba(0,0,0,0.33);
-webkit-font-smoothing:antialiased;
padding:2px 5px;
}

.facetious_submit_button:hover {
background-position:0 0;
background-color:#701;
background-repeat:repeat-x;
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr="#ad001a",endColorstr="#47000a");
background-image:-khtml-gradient(linear,left top,left bottom,from(#ad001a),to(#47000a));
background-image:-moz-linear-gradient(top,#ad001a,#47000a);
background-image:-ms-linear-gradient(top,#ad001a,#47000a);
background-image:-webkit-gradient(linear,left top,left bottom,color-stop(0%,#ad001a),color-stop(100%,#47000a));
background-image:-webkit-linear-gradient(top,#ad001a,#47000a);
background-image:-o-linear-gradient(top,#ad001a,#47000a);
background-image:linear-gradient(#ad001a,#47000a);
border-color:#47000a #47000a #701;
}

 

[转]无插件实现wordpress搜索自定义字段

前段时间有个客户反映,网站搜索搜产品标题能搜出结果,但是如果搜索产品编号(备注:产品编号用自定义字段实现)是搜不出结果的,对于用户的体验不是很好。

确实,文章自定义字段保存在postmeta表中,而wordpress搜索仅会搜索文章表即posts表。

于是google了一下,有现成的解决代码,我就又当一回搬运工。

Search WordPress by Custom Fields without a Plugin

第一步:链接查询

也就是修改搜索查询的sql代码,将postmeta表左链接进去。

/**
 * Join posts and postmeta tables
 *
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_join
 */
function cf_search_join( $join ) {
  global $wpdb;
  if ( is_search() ) {
    $join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
  }
  return $join;
}
add_filter('posts_join', 'cf_search_join' );

第二步:查询代码

在wordpress查询代码中加入自定义字段值的查询。

/**
 * Modify the search query with posts_where
 *
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where
 */
function cf_search_where( $where ) {
  global $pagenow$wpdb;
  if ( is_search() ) {
    $where = preg_replace(
      "/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
      "(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)"$where );
  }
  return $where;
}
add_filter( 'posts_where', 'cf_search_where' );

第三步:去重

搜索结果很有可能有重复的,所以需要去重,很简单,在sql语句中加入DISTINCT关键字。

/**
 * Prevent duplicates
 *
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_distinct
 */
function cf_search_distinct( $where ) {
  global $wpdb;
  if ( is_search() ) {
    return "DISTINCT";
  }
  return $where;
}
add_filter( 'posts_distinct', 'cf_search_distinct' );

总结:

依次将上面三步骤中的代码加入到主题的functions.php文件中即可。

全部代码如下:

<?php
/**
 * Extend WordPress search to include custom fields
 *
 * https://adambalee.com
 */

/**
 * Join posts and postmeta tables
 *
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_join
 */
function cf_search_join( $join ) {
 global $wpdb;

if ( is_search() ) { 
 $join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
 }

return $join;
}
add_filter('posts_join', 'cf_search_join' );

/**
 * Modify the search query with posts_where
 *
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where
 */
function cf_search_where( $where ) {
 global $pagenow, $wpdb;

if ( is_search() ) {
 $where = preg_replace(
 "/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
 "(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where );
 }

return $where;
}
add_filter( 'posts_where', 'cf_search_where' );

/**
 * Prevent duplicates
 *
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_distinct
 */
function cf_search_distinct( $where ) {
 global $wpdb;

if ( is_search() ) {
 return "DISTINCT";
 }

return $where;
}
add_filter( 'posts_distinct', 'cf_search_distinct' );

增强wordpress搜索功能

wordpress自带的搜索功能其实比较简单,搜索的结果就是包含搜索词的文章、页面,按照时间发布顺序排序,下面,我们将介绍如何增强wordpress的搜索,让搜索结果更加精准。

1.增加wordpress搜索的关联性

WordPress本身的搜索结果是按照发布时间排序的,这样的搜索结果的相关性并不强,应该让搜索结果按照内容相关性排序,而不是按照时间或者 ID,所以我们可以在当前主题的 functions.php 添加如下代码来增强 WordPress 搜索的相关性:

if(is_search()){
add_filter('posts_orderby_request', 'search_orderby_filter');
}
function search_orderby_filter($orderby = ''){
    global $wpdb;
    $keyword = $wpdb->prepare($_REQUEST['s']);
    return "((CASE WHEN {$wpdb->posts}.post_title LIKE '%{$keyword}%' THEN 2 ELSE 0 END) + (CASE WHEN {$wpdb->posts}.post_content LIKE '%{$keyword}%' THEN 1 ELSE 0 END)) DESC,
{$wpdb->posts}.post_modified DESC, {$wpdb->posts}.ID ASC";
}

只搜索文章
只搜索文章的标题,将下面的代码添加到主题的 functions.php 文件即可:

/**
 * 让 WordPress 只搜索文章的标题
 */
function __search_by_title_only( $search, &$wp_query )
{
 global $wpdb;
 if ( emptyempty( $search ) )
 return $search; // skip processing - no search term in query
 $q = $wp_query->query_vars;
 $n = ! emptyempty( $q['exact'] ) ? '' : '%';
 $search =
 $searchand = '';
 foreach ( (array) $q['search_terms'] as $term ) {
 $term = esc_sql( like_escape( $term ) );
 $search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
 $searchand = ' AND ';
 }
 if ( ! emptyempty( $search ) ) {
 $search = " AND ({$search}) ";
 if ( ! is_user_logged_in() )
 $search .= " AND ($wpdb->posts.post_password = '') ";
 }
 return $search;
}
add_filter( 'posts_search', '__search_by_title_only', 500, 2 );

选择分类搜索文章:

将默认的searchform.php修改为以下代码:

<form id=”searchform” name=”searchform” method=”get” action=”<?php bloginfo(‘home’); ?>/” >
<ul>
<li>
<p>
<?php $select = wp_dropdown_categories(‘class=search_select&show_option_all=全站搜索& amp;orderby=name&hierarchical=0&selected=-1&depth=1′);?>
</p>
</li>
<li>
<input type=”text” name=”s” id=”s” maxlength=”34″ value=””/>
</li>
<li>
<input type=”image” value=”” src=”<?php bloginfo(‘template_url’); ?>/img/search.gif”/>
</li>
</ul>
</form>

显示出的效果类似下图功能,可选择全站搜索,或者具体的分类搜索,搜索的结果更加精准!

具体样式得自己修改了

多重选项框搜索

这种方法更加强大,可以选择多个分类,并搜索分类中的文章,精确度更好,不过使用这种方法有一定的固定性,你需要自己写好选项框中的分类id:

将你的默认的searchform.php修改为以下代码:

<div> 
<form id=”index_search” name=”index_search” method=”get” action=”<?php bloginfo(‘home’); ?>/”> 
<p><input type=”text” name=”s” id=”s” value=””/> <input type=”submit” value=” 搜 索 ” /></p> 
<p> 
<label for=”s_type5″ style=”width:50px”><input type=”radio” name=”cat” id=”cat” value=”all” checked>全站</label> 
<label for=”s_type1″ style=”width:50px”><input type=”radio” name=”cat” id=”cat” value=”4″ checked>主题</label> 
<label for=”s_type2″ style=”width:50px”><input type=”radio” name=”cat” id=”cat” value=”6″>插件</label> 
<label for=”s_type3″ style=”width:50px”><input type=”radio” name=”cat” id=”cat” value=”3″>主机</label> 
<label for=”s_type4″ style=”width:50px”><input type=”radio” name=”cat” id=”cat” value=”10″>经验</label> 
</p> 
</form> 
</div>

现在,你的wordpress搜索是不是强大了许多呢? 如果不想要使用wordpress的搜索,还可以内置谷歌搜索、百度站内搜索等方式,这些方式可以参阅谷歌百度的官网。

wordpress高级搜索功能示例:按分类搜索

wordpress默认的搜索功能,仅有一个搜索框,功能单一,如何给搜索框增加搜索条件,提高搜索的准确性?

示例:按分类搜索。

让访客可以在搜索的时候,可以选择分类作为条件,同时,本教程也可更改为其它的各种条件。

效果如图:

步骤一:改造搜索框
一般主题都会有一个searchform.php文件,该文件为搜索框的模板,也有的主题没有这个文件,而是使用了wordpress默认的搜索框,要改造搜索框的话,则需要建立searchform.php文件,以便自定义搜索框,阿树使用的searchform.php搜索框的内容和默认的搜索框是一样的,如下:

<form role="search" method="get" class="search-form" action="<?php echo home_url(); ?>">
  <label>
  <span class="screen-reader-text">Search:</span>
  <input type="search" class="search-field" placeholder="Search&hellip;" value="" name="s" title="Search:" />
  </label>
  <input type="submit" class="search-submit" value="Search" />
</form>

里面只有一个文本输入框,及一个提交按钮,我们需要在里面添加一个下拉选择框,先获取所有分类,然后循环将分类输出,如下:

<?php
$args = array(
 'hide_empty' => 0
);
$categories = get_categories( $args );
//上面的代码获取所有分类
?>
<form role="search" method="get" class="search-form" action="<?php echo home_url(); ?>">
 <label>
 <span class="category-text">Category:</span>
 <select name="cat">
 <option value="">All Category</option>
 <?php foreach($categories as $category){ ?>
 <option value="<?php echo $category->term_id; ?>"><?php echo $category->name; ?></option>
 <?php } ?>
 </select>
 </lable>
 <label>
 <span class="screen-text">Search:</span>
 <input type="search" class="search-field" placeholder="Search&hellip;" value="" name="s" title="Search:" />
 </label>
 <input type="submit" class="search-submit" value="Search" />
</form>

如此一来,搜索框中便多了一个分类的下拉框,注意下拉框的name是cat

步骤二:处理搜索条件

接下来就是搜索页面按条件处理搜索了。

网上很多教程都是直接修改搜索页模板search.php来达到目的。那样很不方便,多了很多条件判断不说,还需要另外查询文章,有的甚至直接用数据库操作语句从数据库中查找。

将下面代码添加到functions.php文件中即可:

<?php
function return_only_selected_category( $query ) {
 //is_search判断搜索页面 !is_admin排除后台 $query->is_main_query()只影响主循环
 if ( is_search() && !is_admin() && $query->is_main_query() ){
 //有cat值传入
 if(isset($_GET['cat']){
 $term_id = (int)$_GET['cat']; //处理分类参数
 if( $term_id ){
 //分类查询的参数
 $tax_query = array(
 array(
 'taxonomy'=>'category', //可换为自定义分类法
 'field'=>'term_id',
 'operator'=>'IN',
 'terms'=>array($term_id)
 )
 );
 $query->set( 'tax_query', $tax_query );
 }
 }
 }
 return $query;
}
add_filter('pre_get_posts', 'return_only_selected_category');
?>

通过pre_get_posts钩子去更改查询条件即可。