kenics.net

Technical notes on perl, python, php, sql, cgi, c/c++, q/kdb+, unix/shell, revision control tools, data structures & algorithms, and their applications into web services and other various forms of software engineering.

wordpress notes

 
 
################################ 
###  html meta tags for seo  ### 
################################ 
 
<meta name="author" content="foo bar" /> 
<meta name="description" content="foo bar" /> 
<meta name="keywords" content="foo bar" /> 
 
put enough text/content in a page 
 
use hN tag to structure your content 
 
perma-link 
 
title attribute in anchor tag. 
also text in anchor tag <a href='http://foobar.net'>what goes in here</a> 
 
domain name. and the age of the domain name. 
 
register ping list 
 
 
################################### 
###  movable type VS wordpress  ### 
################################### 
 
<h3>difference table between movable type and wordpress</h3> 
 
         | MT                          | WP 
---------------------------------------------- 
page gen | static                      | dynamic   # static is fast at each access but requires a rebuild every update 
lang     | perl                        | php 
db       | mysql                       | mysql 
open src | n                           | y 
seo      | less                        | lots(especially for google, apparently) 
plugin   | less                        | lots 
license  | not free for commercial use | free 
support  | more                        | less 
security | more                        | prone to be less 
 
 
<h3>difference does not matter to most folks</h3> 
 
There are debates you see on the internet about MT versus WP. I used MT intensively back in 2006, 2007 to manage a website. and now use WP for this website. Frankly, it's really a matter of a preference in essense, just like emacs versus vi. Both are so customizable to the extent that for 98% of the people, it just doesnt matter. 
 
<h3>MT for enterprise/commercial use, and WP for personal website/blog</h3> 
 
in my experience, I think wordpress has become advantageously soooo powerful with the numerous various plugins, and seo feature. it's like with WP you dont have to understand anything technical to accomplish anything because there is an abundance of plugins available only a click away. MT is for people who like developing things. obviously, in terms of security and commercial/enterprise use (including the support concenr), MT is better. besides, that static versus dynamic page generation mechanism is a good example they are intended for different customers(i know WP supports static method too these days). i think most of the people who are running a personal website/blog dont want to have to rebuild the entire thing every time a minor perl module change is applied, etc. 
 
<h3>programmer will prefer MT to WP</h3> 
 
However, in MT's defence, to my geeky engineer mind, MT is far more beautifully structured in a sense that you only write MTML(movable type mark up language) in your template and the functions(perl code) are defined in your plugin, whereas in WP, you mix up everything in both plugin and template. in other words, in WP, in your index.php(the main page template) you might see html/css, php, mysql and maybe javascript all mixed up together. WP tempts people to implement things locally as needed and that ends up in a messy structure. so that design concept of WP is a bit irksome. this, as a practical matter in addition to the afore-mentioned security/support aspects, can be a problem if a team of people need to manage a WP based website. 
 
 
 
################### 
###  plug-ins   ### 
################### 
 
- Syntax Highlighter for WordPress 
i need syntax highlighter when coding. i googled wp syntax highlighter plug-ins and there are quite a few. 
some of the ones i considered after reading reviews. 
 
-synchi 
-WP SyntaxHighlighter 
-Syntax Highlighter for WordPress  # i went with this as the author was the japanese guy and it says multi-language supported. 
 
most of the available ones are based on java-script "Syntax Highlighter" 
i chose the highlight theme "emacs" 
 
- PHP Code for posts 
very useful. lets you use php/html code within your post editor. 
 
 
 
####################### 
###  RSS feed link  ### 
####################### 
 
(URL) 
http://codex.wordpress.org/WordPress_Feeds 
 
<?php bloginfo('rss2_url'); ?>    ## this will give you the RSS xml link 
 
===> usually translates to   http://foobar.com/?feed=rss2 
 
 
###################### 
###  search form   ### 
###################### 
 
(URL) 
http://codex.wordpress.org/Function_Reference/get_search_form 
 
 search is often a very simple form like below. 
 
------------------------------------------------------ 
<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>"> 
 <div> 
  <input type="text" size="20" maxlength="200" style="color:#6E6E6E" value="e.g. sudoku algo" onblur="if( this.value == '' ) { this.value = 'e.g. sudoku algo'; } " onfocus="if( this.value == 'e.g. sudoku algo' ) { this.value = ''; } " name="s" id="s" /> 
  <input type="submit" id="searchsubmit" value="Search" /> 
 </div> 
</form> 
----------------------------------------------------- 
 
===> you just need to pass s="search_string" after home url 
 
(e.g.) 
http://kenics.net/?s=sudoku+algo 
 
## obviously name="s" is whats doing it. 
## id="s" is just an identifier which is not in use in this case. 
 
 
################################### 
###    wp_head()  wp_footer()   ### 
################################### 
 
dont remove these tags cos they are used by plug-ins 
 
<?php wp_head(); ?>      ## used within <head></head>  and inserts lots of <link> tags 
<?php wp_footer(); ?>    ## used just before </body> 
 
 
############################ 
###   native functions   ### 
############################ 
 
the basic approach is to google the description of what you wanna do. anything generic you wanna do is already implemented natively. 95% of any exotic operations can be done by combining the existing functions. 
 
official codex page is the comprehensive source. 
 
i personally reference this web site also. 
(URL) 
http://elearn.jp/wpman/function/get_the_date.html 
 
site_url()    ## wp address you specified in settings-general 
home_url()    ## URL you specified at settings-general-site address(URL) 
 
==> be careful when you use form that submits data in GET method, then that param gets appended. 
 
get_the_date()     ## accepts the same params for php date()  e.g.  get_the_date("Y-m-d H:i:s") 
get_the_content()  ## get the content of the post. 
===> be careful cos it trancates all newlines. recover them as below 
$content = get_the_content(); 
$content = apply_filters('the_content', $content); 
 
wp_specialchars($str, 1)  ## returns HTML char. i.e. "<" is &lt;  ,and whitespace " " is &nbsp; 
 
 
############################################################################# 
####  how to force only name but not email addr when leaving a comment   #### 
############################################################################# 
 
wrt wordpress comment feature, if you make name mandatory then email comes with it. 
but it just so happens you want people leaving comment to give their name(nickname) but email is not needed. 
 
there are ways to hack away wp-comments-post.php, etc, but that always gets overwritten to the default setup everytime you update wordpress version. so instead, a better solution is just adding the below 6 lines in functions.php. 
 
---------------------(add in functions.php)------------------- 
function preprocess_comment_author( $commentdata ) { 
    if ("" === trim( $commentdata['comment_author'] )) 
    wp_die('Pls enter a name'); 
    return $commentdata; 

add_filter('preprocess_comment', 'preprocess_comment_author', 2, 1); 
------------------------------------------------------------ 
 
when you use it, make sure to disable the name/email mandatory buttion in "Settings" - "Discussion" section. 
 
the logic is explained here. 
(URL) 
http://wg.drive.ne.jp/miura/archives/2038 
 
 
######################################## 
#####   how to edit comment form   ##### 
######################################## 
 
comment_form(); 
 
if you dont pass in any param, it gets the default form. so you pass in some $arg as below. 
 
<?php 
 
$commenter = wp_get_current_commenter(); 
$req = get_option( 'require_name_email' ); 
$aria_req = ( $req ? " aria-required='true'" : '' ); 
 
$fields = array( 
    'author' => '<p id="inputtext">' . '<label for="author">' . __( 'Name (required)' ) 
                . ( $req ? '(required)' : '' ) . '</label> ' . 
                '<br /><input id="author" name="author" type="text" value="' 
                . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>', 
    'email'  => '<p id="inputtext"><label for="email">' . __( 'Email (optional)' ) 
                . ( $req ? '(optional)' : '' ) . '</label> ' . 
                '<br /><input id="email" name="email" type="text" value="' 
                . esc_attr(  $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /></p>', 
    'url'    => '<p id="inputtext"><label for="url">' . __( 'Website (optional)' ) . '</label>' . 
                '<br /><input id="url" name="url" type="text" value="' 
                . esc_attr( $commenter['comment_author_url'] ) . '" size="60" /></p>', 
    ); 
 
$args = array( 
    'fields'               => apply_filters( 'comment_form_default_fields', $fields ), 
    'comment_field'        => '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) . '</label><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>', 
    'must_log_in'          => '<p class="must-log-in">' .  sprintf( __( 'You must be <a href="%s">logged in</a> to post a comment.' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>', 
    'logged_in_as'         => '<p class="logged-in-as">' . sprintf( __( 'Logged in as <a href="%1$s">%2$s</a>. <a href="%3$s" title="Log out of this account">Log out?</a>' ), admin_url( 'profile.php' ), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>', 
    'comment_notes_before' => '<p>' . __( 'Your email address will not be published.' ) . '</p>', 
    'comment_notes_after'  => '', 
    'id_form'              => 'commentform', 
    'id_submit'            => 'submit', 
    'title_reply'          => __( 'Leave a comment' ), 
    'title_reply_to'       => __( 'Leave a comment to %s' ), 
    'cancel_reply_link'    => __( 'Cancel comment' ), 
    'label_submit'         => __( 'Post Comment' ), 
); 
 
?> 
 
<?php comment_form($args); ?> 
 
 
############################################# 
####  how to customize comment display   #### 
############################################# 
 
within comments.php, you will see wp_list_comments() which can take in some args for customization. 
 
(URL) 
http://codex.wordpress.org/Function_Reference/wp_list_comments 
 
<?php $args = array( 
    'walker'            => null, 
    'max_depth'         => '', 
    'style'             => 'ul', 
    'callback'          => 'mytheme_comment', 
    'end-callback'      => null, 
    'type'              => 'all',      ## all includes comment/trackback/pingback you can do "comment" only 
    'page'              => '', 
    'per_page'          => '', 
    'avatar_size'       => 32,   ## this is for avatar pic size 
    'reverse_top_level' => null, ## if true, then latest comment appear on top 
    'reverse_children'  => '', 
'reply_text' => 'Reply to this comment<br>', 
'login_text' => 'Login to reply to this comment' 
    ); 
?> 
 
<ul class="commentlist"> 
 <?php wp_list_comments($args); ?> 
</ul> 
 
 
-----------(add this in functions.php)---------- 
function mytheme_comment($comment, $args, $depth) { 
   $GLOBALS['comment'] = $comment; ?> 
   <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>"> 
     <div id="comment-<?php comment_ID(); ?>"> 
      <div class="comment-author vcard"> 
         <?php echo get_avatar($comment,$size='48',$default='<path_to_url>' ); ?> 
 
         <?php printf(__('<cite class="fn">%s</cite> <span class="says">says:</span>'), get_comment_author_link()) ?> 
      </div> 
      <?php if ($comment->comment_approved == '0') : ?> 
         <em><?php _e('Your comment is awaiting moderation.') ?></em> 
         <br /> 
      <?php endif; ?> 
 
      <div class="comment-meta commentmetadata"><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>"><?php printf(__('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?></a><?php edit_comment_link(__('(Edit)'),'  ','') ?></div> 
 
      <?php comment_text() ?> 
 
      <div class="reply"> 
         <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?> 
      </div> 
     </div> 
<?php 
        } 
--------------------------------------------- 
 
 
######################### 
####   get_avatar()   ### 
######################### 
 
(URL) 
http://codex.wordpress.org/Function_Reference/get_avatar 
 
 
-------------(add this in functions.php)------------------ 
function newgravatar ($avatar_defaults) { 
    $myavatar = get_bloginfo('template_directory') . '/images/your_new_mystery_man.jpg'; 
    $avatar_defaults[$myavatar] = "name_of_your_new_avatar_icon"; 
    return $avatar_defaults; 

add_filter( 'avatar_defaults', 'newgravatar' ); 
--------------------------------------------------------- 
 
 
 
################################# 
###  how to get recent posts  ### 
################################# 
 
(URL) 
http://codex.wordpress.org/Function_Reference/wp_get_recent_posts 
 
------------------------------- 
<h2>Recent Posts</h2> 
<ul> 
<?php 
$recent_posts = wp_get_recent_posts(); 
foreach( $recent_posts as $recent ){ 
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> </li> '; 

?> 
</ul> 
-------------------------------- 
 
 
############################# 
####   list categories   #### 
############################# 
 
wp_list_categories() 
 
(URL) 
http://codex.wordpress.org/Function_Reference/wp_list_categories 
 
------(example usage)------ 
<ul> 
<?php wp_list_categories('title_li=List of all categories'); ?> 
</ul> 
---------------------------- 
 
=====> it will display as below 
 
- List of all categories 
  - perl 
  - unix 
  - quotes 
 
 
############################################## 
###   how to embed external HTML content   ### 
############################################## 
 
(URL) 
http://codex.wordpress.org/Function_API/wp_remote_get 
http://codex.wordpress.org/Function_Reference/wp_remote_post 
 
wp_remote_get($URL[,$arg]);   ## you just put an URL and it retrieves the src code. 
 
------------------------------------------- 
<?php 
$response = wp_remote_get('http://web.sfc.wide.ad.jp/~mel/note.cgi?en_quote'); 
print_r( $response['body'] ); 
?> 
------------------------------------------- 
 
## related functions  ## get_file_contents($URL),  wp_remote_post($URL); 
 
an alternative way is to use <iframe> but it has lots of restriction cos it's just an embedded html where you have no control over css. 
 
 
 
############################################################################## 
####   when wordpress goes completely white after editting functions.php   ### 
############################################################################## 
 
it's a known issue for wordpress to go down if you have illegal space character and save. 
just FTP into your wp directory and manually re-edit whatever file you were editting. 
 
 
 
################################################################# 
###  cmd we can use inside  have_posts() : the_post()  loop   ### 
################################################################# 
 
the_title() 
the_content() 
the_permalink() 
the_author() 
the_date() 
the_ID()  // post id 
 
################################################## 
###   difference between get_<cmd> and <cmd>   ### 
################################################## 
 
at first, it's very confusing when get_<cmd> and <cmd> are used seemingly inter-changably. 
look at the wordpress codex spec/definition for each case. some param, return style differences, etc. 
 
get_the_title()   // can be used outside a loop. also we can specify the post ID as a param. 
the_title()       // can be used only inside a loop. also prints by default. 
 
===> often one is the wrapper around the other. the_title uses get_the_title() inside. 
 
 
 
 
 
##################################################### 
###   how to implement a custom search function   ### 
##################################################### 
 
we use WP_Query("posts_per_page=-1") to get all posts. and for each content we do grep search-string using php regex preg_match(). 
 
(URL) 
http://codex.wordpress.org/Function_Reference/WP_Query 
 
WP_Query() is very useful. read the doc. 
in the argument, you can specify by author, post, category, page, taxonomy, etc. 
 
$query = new WP_Query( 's=foobar&cat=2,6,17,38&posts_per_page=-1' ); 
 
//  gets all posts from category 2,6,17,38 && contain "foobar" 
//  "posts_per_page=-1" means get all the posts into one page. 
 
you use the loop cmd (example below) to iterate and access each post. 
 
e.g. 
------------------------------------ 
<?php 
 
// query 
$the_query = new WP_Query( $args ); 
 
// loop 
while ( $the_query->have_posts() ) : $the_query->the_post(); 
 echo '<li>'; 
 the_title(); 
 echo '</li>'; 
endwhile; 
 
//reset 
wp_reset_postdata(); 
 
?> 
------------------------------------ 
 
 
 
----------------------[ real search.php example ]-------------------------- 
 
// get all posts 
<?php $allsearch =& new WP_Query("posts_per_page=-1"); 
$key = wp_specialchars($s, 1); 
$count = 0; 
?> 
 
// prepare html tag for listing the search result 
<ul> 
Published Date | Category | Article Title 
<br>--------------------------------------<br> 
 
// begin the search loop 
<?php if ($allsearch->have_posts()) : ?> 
           <?php while ($allsearch->have_posts()) : $allsearch->the_post(); ?>  
           <?php $cat = get_the_category(); $cat = $cat[0]; ?> 
<?php 
$content = get_the_content(); 
$content = apply_filters('the_content', $content); 
 
// grep for the keyword in content, title, category 
if (preg_match("/${key}/i",$content) || 
    preg_match("/${key}/i",get_the_title()) || 
    preg_match("/${key}/i",get_the_category()) ) : 
$count++; 
?> 
 
// if hit, then just print out the date, category, post-name 
<li><?php echo get_the_date() ?> | <a href="<?php echo home_url().'/?cat='.$cat->cat_ID ?>"><?php echo $cat->cat_name; ?></a> | <a href="<?php the_permalink(); ?>"> <?php echo get_the_title();?> </a> 
        </li> 
 
<?php endif; ?> 
<?php endwhile; ?> 
<?php endif; ?> 
// if no hit, just print "no entry found" 
<?php if($count == 0) : ?> 
               <li>no entry found</li> 
<?php endif; ?> 
 
--------------------------------------<br> 
        </ul> 
Result: <?php echo $count ?> entries found. <br> 
 
----------------------[ real search.php example ]------------------------ 
 
 
=========> the above example is using a purely sequential grep search. if the total content volume grows, then a more sophisticared search mechanism such as index based search is required. 
 
 
 
 
##################### 
###   FTP client  ### 
##################### 
 
anything is fine really. i use winscp. 
 
##################### 
####  ping list  #### 
##################### 
 
this is just a notifier function of your blog engine to ping certain web sites. 
 
i got the below list from these web sites. 
 
http://kobotake.com/ping 
http://seo.siyo.org/ping/seo8449/ 
 
----   ping list  ----- 
 
http://api.my.yahoo.co.jp/RPC2 
http://blog.goo.ne.jp/XMLRPC 
http://blog.with2.net/ping.php/ 
http://blogrolling.com/ 
http://blogsearch.google.co.jp/ping/RPC2 
http://blogsearch.google.com/ping/RPC2 
http://hamham.info/blog/xmlrpc/ 
http://jugem.jp/?mode=NEWENTRY 
http://ping.ask.jp/xmlrpc.m 
http://ping.bloggers.jp/rpc/ 
http://ping.blogmura.jp/rpc/ 
http://ping.blogram.jp/rpc/127385 
http://ping.blogranking.net/ 
http://ping.cocolog-nifty.com/xmlrpc 
http://ping.dendou.jp/ 
http://ping.exblog.jp/xmlrpc 
http://ping.fc2.com/ 
http://ping.feedburner.com 
http://ping.freeblogranking.com/xmlrpc/ 
http://ping.rootblog.com/rpc.php 
http://ping.rss.drecom.jp/ 
http://ping.sitecms.net 
http://pingoo.jp/ping/ 
http://ranking.kuruten.jp/ping 
http://rpc.blogrolling.com/pinger/ 
http://rpc.pingomatic.com/ 
http://rpc.reader.livedoor.com/ping 
http://rpc.technorati.com/rpc/ping 
http://rpc.weblogs.com/RPC2 
http://serenebach.net/rep.cgi 
http://taichistereo.net/xmlrpc/ 
http://wpdocs.sourceforge.jp/Update_Services 
http://www.bloglines.com/ping 
http://www.blogoole.com/ping/ 
http://www.blogpeople.net/servlet/weblogUpdates 
http://www.i-learn.jp/ping/ 
http://xping.pubsub.com/ping/ 
 
------------------------- 
 

  1. 2014-06-19 18:28:50 |
  2. Category : wordpress
  3. Page View:

Google Ads