EspañolCatalàEuskaraEnglish
Information Technology

Grouping shortcodes in a plugin

How many times has happened to change the subject that we have been left with a ticket shorcode between? To my many… so I decided to start my own creating me shortcodes could be used for all threads.

The process of creating a plugin is quite simple and I will not extend in documenting, as it is well explained in “Writing a Plugin” WordPress documentation.

Essentially this will be:

  • Choose a name for the plugin, try to be unique and it podermos Google it to check its nonexistence. We can always give you an identifier before the name, e.g. “TopEinfo ShortCode Pack”
  • Create a folder and a php file inside with the name chosen, but, attention, we will use lowercase unaccented and we will change all symbols by hyphens “-“, WordPress as it does with the url of our posts or pages. In our case would: “topeinfo-shortcode-pack”.
  • Edit the file php

And our code could start:

<?php
/*
Plugin Name: TopEinfo ShortCode Pack
Plugin URI:
Description: Conjunto de shortcodes y útilidades varias
Author: Joan López
Version: 1.0
Author URI: https://www.topeinfo.es/
*/

Then we want to create the shortcodes, e.g., one of my favorites used to insert the contents of a page on any site


/**
 * Inserta el contenido de una página
 *
 * Atributos:
 * - id: id de la página
 * - name: título de la página o slug
 * - reduce: (false) si es true, reduce los encabezados (h1->h2, h2->h3 ...)
 *
 * @param array $atts (id: int, name:string)
 * @return type
 * @version 1.05.20130216
 */
 function topeinfo_utils_pagina($atts, $content='') {
 $id = '';
 $reduce = false;
 extract(shortcode_atts(array(
 'id' => 0,
 'name' => '',
 'reduce' => false,
 'class' => ''
 ), $atts));

if ($id==0) {
 $page_data = get_page_by_path($name);
  if ($page_data == null)
  $page_data = get_page_by_title($name);
 }
 else
  $page_data = get_page($id);

if ($page_data == null)
 return $content;

$contenido = apply_filters('the_content', $page_data->post_content);
 if ($reduce == 'true' || $reduce == 1) {
  $contenido = str_ireplace('h5', 'h6', $contenido);
  $contenido = str_ireplace('h4', 'h5', $contenido);
  $contenido = str_ireplace('h3', 'h4', $contenido);
  $contenido = str_ireplace('h2', 'h3', $contenido);
  $contenido = str_ireplace('h1', 'h2', $contenido);
 }

 if (!empty($class)) {
 $contenido = "<div class=\"{$class}\">{$contenido}</div>";
 }

return $contenido;
 }

Now, must be informed that a new WordPress shortcode available for use and we will use the function add_shortcode:

add_shortcode('pagina', 'topeinfo_utils_pagina');

And so we can go together all the shortcodes that we go through the head.

Tag Cloud

Your browser doesn't support canvas.

Categories