顯示具有 快快樂樂學Wordpress 標籤的文章。 顯示所有文章
顯示具有 快快樂樂學Wordpress 標籤的文章。 顯示所有文章

星期二, 11月 15, 2016

[Wordpress] 讓所有頁面導向具體頁面

如果想把全站的page導向某一頁的話可以在functions.php加入一個action:
 template_redirect



function redirect_page_standard() {
  wp_redirect(  get_site_url() . '/notice.html', 301 );

}
add_action( 'template_redirect', 'redirect_page_standard' );

星期三, 9月 28, 2016

星期三, 8月 31, 2016

[Wordpress] wp_users 與 wp_usermeta tables取值的key

記錄一下常用到get_userdata與get_user_meta常要用取值key的值!!!
  • users
    • ID
    • user_login
    • user_pass
    • user_nicename
    • user_email
    • user_url
    • user_registered
    • display_name
  • user_meta
    • first_name
    • last_name
    • nickname
    • description
    • wp_capabilities (array)
    • admin_color (Theme of your admin page. Default is fresh.)
    • closedpostboxes_page
    • primary_blog
    • rich_editing
    • source_domain

星期四, 8月 18, 2016

[Wordpress] sql查詢特色圖片(featured images)

如果要查詢縮圖的資訊直接查詢postmeta這張表_wp_attached_file這個meta_key即可。文章縮圖的id則為_thumbnail_id

[Wordpress] 使用自訂的sql抓取WMPL翻譯的文章類型

記錄如何自已下sql取得文章的多國類型,裝的外掛為WMPL。
請注意WMPL此外掛是將翻譯的關連資訊存到mywp_icl_translations



以下為sql語句


select
posts.ID,
posts.post_name,
posts.post_date,

IFNULL((SELECT PM.meta_value FROM mywp_postmeta AS PM WHERE posts.ID = PM.post_id AND PM.meta_key = "your_post_type"),'') as link,
IFNULL((SELECT PM.meta_value FROM mywp_postmeta AS PM WHERE posts.ID = PM.post_id AND PM.meta_key = "_thumbnail_id"),'') as thumbnail_id

FROM mywp_posts AS posts
where
post_status = 'publish'
AND
post_type = 'emel_ad_slider'
AND
ID
IN
(
select element_id from mywp_icl_translations
where element_type = 'post_your_post_type' and language_code = 'zh-hans'
)
order by posts.post_date desc

星期一, 8月 08, 2016

[Wordpress] WooCommerce 自訂頁面呼叫相關API


今天要在自訂的php頁面呼叫wc的api記錄,因為老闆說要另外做一頁就這樣搞了XD不要問

載入WP的函式庫

require_once(dirname(dirname(__FILE__)).'/wp-load.php');


星期日, 8月 07, 2016

[Wordpress] 取出指定user meta值的用戶清單


這次想要在wordpress的帳號管理的查詢功能新增自已的user meta key查詢。
使用到的Hook分別為如下:

  • restrict_manage_users ACTION:

這個Action可以替users表格上面的filter區塊自訂我們要的html 語法,這次要做的效果一樣弄一個下拉式選單就OK了。送出後你會發現網址QueryString會加了我們指定的值

// Adds a dropdown to filter users based on a meta field
function add_recommand_filter_into_user_table() {

    global $pagenow;

    if (is_admin() && $pagenow == 'users.php') {
        $optNone = '';
        $optYes = '';
        $optNo = '';
        if(isset($_GET['recommand'])){
          if(strcmp($_GET['recommand'],'1') == 0){
            $optYes  = 'selected="selected"';
          }else{
            $optNo  = 'selected="selected"';
          }
        }else{
           $optNone  = 'selected="selected"';
        }

        echo '';
    }
}
add_action('restrict_manage_users', 'add_recommand_filter_into_user_table');



  • pre_get_users FILTER:


這個Filter簡言之就是要處理我們新增的action所送出的值為何,然後可以重改變動query物件的meta參數
// Updates user query based on filtering criteria
function query_recommand_query_from_user_table($query) {
 
    global $pagenow;

    if (is_admin() && $pagenow == 'users.php' && isset($_GET['recommand'])) {
        $recommandValue = wp_strip_all_tags($_GET['recommand']);
       
       if(strcmp($recommandValue,'') != 0){
          $meta_query = array(
            array(
           'key' => RECOMMAND_KEY,
           'value' => $recommandValue
           )
         ); 
   
         $query->set('meta_key', RECOMMAND_KEY);
         $query->set('meta_query', $meta_query);
        }   
    }
 
}

add_filter('pre_get_users','query_recommand_query_from_user_table');


參考


  1. http://wordpress.stackexchange.com/questions/189077/how-to-get-users-by-a-custom-field-by-user-meta-data
  2. http://www.davemccourt.com/wp-user-filtering/

星期一, 8月 01, 2016

[Wordpress] 擴充User management 的表格欄位

如果你想要自已擴充全部帳號(users.php)頁面,只需要透過二個hook機制就可以快速完成。
所以你就可以自已加欄位加按鈕,做更多客製化操作啦。


  • 使用filter hook: manage_users_columns來擴充欄位
  • 使用action hook: manage_users_custom_column來修改欄位值

範例


參考

https://www.tipsandtricks-hq.com/adding-a-custom-column-to-the-users-table-in-wordpress-7378

[Wordpress] 如何ajax來呼叫 php function

記錄一下如何在wp操作ajax function呼叫php function,意思就是你可以透過hook機制寫自已的api嚕

官網的sample說明


  • action命名規則wp_ajax_[你的方法]
  • action function 命名規則 [你的方法]_callback
tips: 請注意你的方法命名不可以長的不一樣,會無法對應呼叫

add_action( 'wp_ajax_my_action', 'my_action_callback' );

function my_action_callback() {
 global $wpdb; // this is how you get access to the database

 $whatever = intval( $_POST['whatever'] );

 $whatever += 10;

        echo $whatever;

 wp_die(); // this is required to terminate immediately and return a proper response
}

在頁面render一個jquery的方法(因為是範例所以方法很簡單,page_load之後就打)可以搭配admin_enqueue_scripts來整理你自已外掛所需的js
 jQuery(document).ready(function($) {

  var data = {
   'action': 'my_action',
   'whatever': 1234
  };

  // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
  jQuery.post(ajaxurl, data, function(response) {
   alert('Got this from the server: ' + response);
  });
 });

參考

https://codex.wordpress.org/AJAX_in_Plugins

[Wordpress] wp_enqueue_script / wp_enqueue_style 在wp自訂你的js與css檔

wp_enqueue_script 允許我們快速的在wp的頁面加入js函式庫,
反之 wp_enqueue_style讓我們加入樣式檔。

你可以寫法functions.php做全域的載入或至到外掛模組佈景主題裡面做客製化的載入唷

範例

以下範例教你怎麼加入bootstrap框架

function reg_scripts() {
    wp_enqueue_style( 'bootstrapstyle', get_template_directory_uri() . '/css/bootstrap.min.css' );
    wp_enqueue_style( 'bootstrapthemestyle', get_template_directory_uri() . '/css/bootstrap-theme.min.css' );
    wp_enqueue_script( 'bootstrap-script', get_template_directory_uri() . '/js/bootstrap.min.js', array(), true );
}
add_action('wp_enqueue_scripts', 'reg_scripts');

參考


  1. https://digwp.com/2009/06/including-jquery-in-wordpress-the-right-way/
  2. http://stackoverflow.com/questions/26583978/how-to-load-bootstrap-script-and-style-in-functions-php-wordpress
  3. https://codex.wordpress.org/zh-cn:%E5%87%BD%E6%95%B0%E5%8F%82%E8%80%83/wp_enqueue_script

星期三, 4月 27, 2016

[快快樂樂學WordPress] 記錄一些wordpress plugin需要用的資料

Google到的Wordpress plugin教學蒐集

//好像很老的玩家lol
http://blog.wpjam.com/article/write-plugin-by-yourself/

//官方
http://codex.wordpress.org/Writing_a_Plugin
http://codex.wordpress.org/Adding_Administration_Menus
http://codex.wordpress.org/Plugin_API

星期日, 3月 27, 2016

[快快樂樂學Wordpress] wordpress來指定某個用戶登入以實作SSO(單點登入)

最近實作了一個wp的SSO機制,
主要用了get_user_by的方法來取得用戶的資訊後,
再透過wp_set_current_user與wp_set_auth_cookie設定用戶的session並執行wp_login的hook即可。

if($user){

   //do the single sing on
   wp_set_current_user( $user_id, $user->user_login );
      wp_set_auth_cookie( $user_id );
      
      //email
      // echo $user->user_login;

      do_action( 'wp_login', $user->user_login,$user );
  }
 

REF

http://carlofontanos.com/auto-login-to-wordpress-from-another-website/

星期五, 3月 04, 2016

[快快樂樂學Wordpress] 自訂樣版: 基礎架構 (1)

記錄一下新增wordpress樣版的筆記


樣版基本需求檔案

請在theme folder之下新增下面三個檔案

index.php
style.css
function.php

之後後台就會出現你的樣版了



DEMO

因為index.php與function.php什麼都沒寫,因此網站會呈現一片空白 Orz

完成的佈景主題所需要的檔案

 style.css

 header.php

 footer.php

 index.php

 single.php

 page.php

 sidebar.php

 comments.php

 archives.php

 search.php

 searchform.php

 404.php

 functions.php





其他你感興趣的文章

Related Posts with Thumbnails