Wordpress

From eBabel wiki
Jump to: navigation, search

Create a custom Wordpress theme

Start from the Whiteboard theme and build your own.

Install Wordpress

Source: Official installation of Wordpress.

Install the Wordpress files

<syntaxhighlight lang="vim" line>

cp ~/Dropbox/install/wordpress-3.4.1.zip /var/www/eu.ebabel.www/
cd /var/www/eu.ebabel.www/
unzip wordpress-3.4.1.zip
rm wordpress-3.4.1.zip
mv wordpress/* .
rmdir wordpress/

</syntaxhighlight>

Create a MySql database

<syntaxhighlight lang="mysql" line>

mysql information_schema --user=rootuser --password=rootpassword
create database ebabel;
show databases;

</syntaxhighlight>

Create a user for the MySql database

<syntaxhighlight lang="mysql" line>

grant all on ebabel.* to newusername@localhost identified by 'newpassword';
exit

</syntaxhighlight>

Rename the wp-config-sample.php file to wp-config.php

<syntaxhighlight lang="vim" line>

mv wp-config-sample.php wp-config.php

</syntaxhighlight>

Place the database settings in wp-config.php

The settings are the name of the database you have just created, its username and password.

<syntaxhighlight lang="vim" line>

gedit wp-config.php

</syntaxhighlight>

Generate SALT keys

The same wp-config.php has several salt keys to be generated

Replace the default text by those generated keys, save and close wp-config.php

Run the installation script

Change all files to be owned by www-data, so that the web based scripts have full control when run from a browser.

<syntaxhighlight lang="vim" line>

sudo chown -R www-data:www-data /var/www/eu.ebabel.www/

</syntaxhighlight>

Browse to:

http://ebabel.loc/wp-admin/install.php


Custom themes

Create a custom page template

Add a new php page with a comment at the top. Example below is a new page template called "Contact".

<syntaxhighlight lang="php" line>

<?php
/**
 * Template Name: Contact
 */
?>

</syntaxhighlight>

Display the main content of a page in a template

<syntaxhighlight lang="php" line>

<?php while ( have_posts() ) : the_post(); ?>
   <?php get_template_part( 'content', 'page' ); ?>
<?php endwhile; ?>

</syntaxhighlight>

The default file that contains the page content template is content-page.php

Display standard menu of public pages, including sub-pages

<syntaxhighlight lang="php" line>

<?php wp_nav_menu(); ?>

</syntaxhighlight>

List the public pages in a custom theme

<syntaxhighlight lang="php" line>

<?php wp_list_pages('title_li='); ?>

</syntaxhighlight>

or

<syntaxhighlight lang="php" line>

<?php $args = array(
	'depth'        => 0,
	'show_date'    => ,
	'date_format'  => get_option('date_format'),
	'child_of'     => 0,
	'exclude'      => ,
	'include'      => ,
	'title_li'     => __('Pages'),
	'echo'         => 1,
	'authors'      => ,
	'sort_column'  => 'menu_order, post_title',
	'link_before'  => ,
	'link_after'   => ,
	'walker'       => ,
	'post_type'    => 'page',
       'post_status'  => 'publish' 
 ); 
 wp_list_pages( $args );
?>

</syntaxhighlight>

Empty or default parameters are not compulsory, so the long array above is equivalent to:

<syntaxhighlight lang="php" line>

<?php $args = array(
	'title_li'     => __('Pages'),
 ); 
 wp_list_pages( $args );
?>

</syntaxhighlight>

See also Wordpress reference on listing public pages.

Include comments in a custom template

To enable comments on a public page using a custom template, add:

<syntaxhighlight lang="php" line>

<?php comments_template( , true ); ?>

</syntaxhighlight>

Database operations with $wpdb

Wordpress $wpdb reference documentation.

Write data to a custom table with Wordpress wpdb

<syntaxhighlight lang="php" line>

<?php
require_once('/wp-config.php');
global $wpdb;

$wpdb->insert( 
	'tablename', 
	array( 
		'column1' => 'data', 
		'column2' => 'moredata'
	)
);
?>

</syntaxhighlight>

Update database table with Wordpress

The formats accepted to determine a datatype are:

<syntaxhighlight lang="php">

'%d', '%f', '%s' (integer, float, string)

</syntaxhighlight>


<syntaxhighlight lang="php" line>

$wpdb->update( 
   'tablename', 
   array(
      'stringcolumn' => $data->stringparameter,
      'numericcolumn' => $data->numericcolumn,
      'floatcolumn' => $data->floatcolumn
   ), 
   array(
      'wherecolumn1' => $data->id1,
      'wherecolumn2' => $data->id2
   ), 
   array( '%s', '%d', '%f' ), 
   array ( '%d', '%d' )
);

</syntaxhighlight>

Read data from the Wordpress database

<syntaxhighlight lang="php" line>

$result = $wpdb->get_results("SELECT columnname FROM tablename WHERE columnname = value");

</syntaxhighlight>

However it is safer to use $wpdb->prepare() with parameters.

<syntaxhighlight lang="php" line>

$result = $wpdb->get_results($wpdb->prepare(
   "SELECT columnname FROM tablename WHERE columnname1 = %d AND columnname2 = %d", value1, value2));

</syntaxhighlight>

Delete data from the Wordpress database

Using the generic $wpdb->query() method must warrant some extra precautions to prevent SQL injection attacks.

<syntaxhighlight lang="php" line>

$wpdb->query($wpdb->prepare(
 "DELETE FROM tablename WHERE columnname1 = %d AND columnname2 = %d",value1, value2 ));

</syntaxhighlight>

Tips

jQuery

In Wordpress, various plugins may create more than one instance of $ and jQuery. To solve this issue and be able to continue using $ and not write jQuery, before writing your own custom jQuery code, place:

<syntaxhighlight lang="javascript" line>

var $ = jQuery.noConflict();

</syntaxhighlight>