Herramientas de usuario

Herramientas del sitio


wiki2:wordpress

¡Esta es una revisión vieja del documento!


Wordpress

Basic

To install

  • You only have to download the source code and extract it into the chosen folder, the web server must have permissions to write on it.
  • You have to create a data base on the MySql server for Wordpress. When you access to it on the first time, the webpage will show you the installer; you will have to introduce the data.

Directory structure

  • Root directory. The only useful file is wp-config.php.
  • wp-content. Contain plugins, themes, and uploaded files.
    • wp-content/plugins. Plugins.
    • wp-content/themes. Installed themes.
    • wp-content/uploads. Uploaded media.
    • wp-content/mu-plugins. This directory doesn't exist unless you create it. Mu means must use. Any plugin you put here will be automatically run without needing to activate it.
  • wp-admin. To manage the admin.
  • wp-inludes. Core directory.

How to develop

Core code files implement main Wordpress functions. /wp-includes/option.php, for example, contains functions for updating the site configuration, /wp-includes/pluggable.php and /wp-includes/ user.php contain function for updating users… Also exist /wp-includes/post.php, comments…

There is also the possibility to add more information to these elements (posts, users, comments…); to do that exists the concept of meta elements, which exists for other main elements in Wordpress.

Themes and plugins

Themes

wp-content\themes contains several folders, each one is an installed theme. You can develop your theme there.
WordPress themes only require two files: index.php and style.css. They usually are made up of many files and can become quickly disorganized. Still Wordpress does not need any folder you also can add a js and a css folders, in its root you'll place the template main files. There's also another especial folder, page-templates, where you will put the custom page template files.

So, to develop a theme you will create a folder with the name of the theme. There you will add an index.php file and a style.css.

Theme hierarchy

WordPress uses the query string (a sequence of codes in a Uniform Resource Identifier (URI) that a web page uses to determine what dynamic data to display) to decide which template or set of templates should be used to display the page.

  1. From the query string Wordpress decides which page is being requested (search, category…).
  2. Selects the template determined by the template hierarchy.
  3. Looks for template files with specific names in the current theme’s directory and uses the first matching template file as specified by the hierarchy.

With the exception of the basic index.php template file, you can choose whether you want to implement a particular template file or not. If WordPress cannot find a template file with a matching name, it will skip to the next file in the hierarchy. If WordPress cannot find any matching template file, the theme’s index.php file will be used.

  • Home Page (home.php | index.php). By default, WordPress sets your site’s home page to display your latest blog posts. This page is called the Blog Posts Index. The template file home.php is used to render the blog posts index, whether it is being used as the front page or on separate static page. If the home.php file does not exist, WordPress will use the index.php file. If the front-page.php file exists, it will override the home.php file.
  • Front Page (font-page.php | home.php | page.php | index.php). The front-page.php template file is used to render your site’s Front Page, whether the front page displays the blog posts index or a static page.
  • Single post (single-{post-type}.php | single.php | index.php). Template file used to render a single post.
  • Page (custom template file | page-slug.php | page-id.php | index.php). The template file used to render a static page (page post-type).
  • Category.
  • Tags
  • Custom taxonomies.
  • Custom post types.
  • Author display.
  • Date.
  • Search Result.
  • 404 not found.
  • Attachment.

Attaching CSS and JS

The style.css and the comment-reply script are required in all themes, but it may be necessary to add other files to extend the functionality of your theme. There are two steps to use scripts and styles in your template. First to register and second to enqueue it in the functions.php files.

// register a stylesheet and a script
wp_register_style( $handle, $src, $deps, $ver, $media );
wp_register_script( $handle, $src, $deps, $ver, $in_footer );
// enqueue a style and a scriptt
wp_enqueue_style( $handle );
wp_enqueue_script( $handle );
  • $handle — is the reference for the script or style and is used in the enqueue function.
  • $src — URL to the file you are registering.
  • $deps — array of any dependencies (optional).
  • $ver — indicated version (optional)
  • $media — for style indicates the media type the script is intended for (optional).
  • $in_footer — for scripts places the scrip in the footer (optional).

There are some of them already registered.

Rather then loading the stylesheet in your header.php file, you should load it in using wp_enqueue_style. In order to load your main stylesheet, simply insert:

wp_enqueue_style( 'style', get_stylesheet_uri() );
wp_enqueue_style( 'slider', get_template_directory_uri() . '/css/slider.css',false,'1.1','all');
wp_enqueue_script( 'script', get_template_directory_uri() . '/js/script.js', array ( 'jquery' ), 1.1, true);

Page templates

Special wordpress templates. They are only to change the look and feel of a page; they can be applied to a single page, a page section or a class of pages. page-about.php is a page template, more specific than page.php or index.php.

The post loop

It's used to display posts according to your instructions given by template tags. The basic loop is (while there are posts, display the posts):

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

The most simple index.php would be:

<?php
get_header();
if (have_posts()) :
   while (have_posts()) :
      the_post();
      the_content();
   endwhile;
endif;
get_sidebar();
get_footer();
?>

Other example:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
 
    <?php the_post_thumbnail(); ?>
    <?php the_excerpt(); ?>
 
<?php endwhile; else: ?>
<?php _e('Sorry, no posts matched your criteria.'); ?>
<?php endif; ?>

Functions that cna be used

WP_Query

WP_Query is a class given by WordPress to be used in the loop. For example $wp_query→have_posts() is called to see if there are any posts to show. To obtain a post $wp_query→the_post() is called. Also can be used to filter posts:

<?php $my_query = new WP_Query( $args ); ?>
<?php while ($my_query->have_posts()) ? $my_query->the_post(); ?>
    // Do something
<?php endwhile; ?>

Conditional tags

They are used o control which templates are loaded on a certain page. Also which parts:

Useful functions to use on templates

  • wp_head(), put it before </head> and wordpress will add required code for plugins.
  • get_footer() includes the footer.php template file. get_header() includes the header.php template file. get_sidebar() includes the sidebar.php. Customized versions of these files can be called as well by naming the file sidebar-{your_custom_template}.php… You can then call the get_* functions with the custom template name as the only parameter, like this: get_header( 'your_custom_template' );.
  • get_template_directory_uri(); & get_stylesheet_directory_uri();
  • Conditional functions: is_user_logged_in(), is_home(), is_front_page(), is_admin()
  • bloginfo('name'); to obtain information about the blog (name, title…)
  • the_title();

Plugins

wiki2/wordpress.1420229376.txt.gz · Última modificación: 2020/05/09 09:25 (editor externo)