Tipos personalizados

WordPress permite crear tipos de datos personalizados. Igual que tenemos entradas o páginas podemos añadir tipos propios e incluso taxonomías y campos personalizados. Esto lo podemos hacer de varias maneras, a pelo desde el código o usando un plugin. Para esta ocasión usaremos el siguiente plugin:

https://es.wordpress.org/plugins/types/

Que tiene una abundante documentación:

https://toolset.com/documentation/

https://toolset.com/documentation/user-guides/create-a-custom-post-type/

Widget El tiempo

<?php
/*
  Plugin Name: El Tiempo
  Plugin URI: http://intelisen.com/plugins
  Description: Un plugin para ver el tiempo
  Author: Intelisen
  Version: 1
  Author URI: http://intelisen.com
 */

class EltiempoWidget extends WP_Widget {

    function EltiempoWidget() {
        $widget_ops = array('classname' => 'EltiempoWidget', 'description' => 'Muestra el tiempo');
        $this->WP_Widget('EltiempoWidget', 'Muestra el tiempo', $widget_ops);
    }

    function form($instance) {
        $instance = wp_parse_args((array) $instance, array('title' => ''));
        $title = $instance['title'];
        ?>
        <p><label for="<?php echo $this->get_field_id('title'); ?>"><?= _e('Title') ?>: <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p>
        <?php
    }

    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['title'] = $new_instance['title'];
        return $instance;
    }

    function widget($args, $instance) {
        extract($args, EXTR_SKIP);

        echo $before_widget;
        $title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);

        if (!empty($title))
            echo $before_title . $title . $after_title;

        // CóDIGO DEL WIDGET
        // Llamo a la api 'ip', le paso la ip que quiero buscar y me devuelve un json
        // con los datos de la localización (http://ip-api.com/docs/api:json#usage)
        //$a = json_decode(file_get_contents('http://ip-api.com/json/'.$_SERVER['REMOTE_ADDR']));
        $a = json_decode(file_get_contents('http://ip-api.com/json/'));
        if (!empty($a)) {
            $ciudad = $a->city;
            //Obtengo el tiempo a partir de una ciudad. Necesito una clave
            //Le paso la ciudad y la clave (https://openweathermap.org/current)
            $b = json_decode(file_get_contents('https://api.openweathermap.org/data/2.5/weather?q=' . $a->city . '&appid=db13c3a29d4018cca5e6ef261442cc64'));
            if (!empty($b)) {
                echo "<p>Máxima:" . ($b->main->temp_max - 273) . "</p>";
                echo "<p>Mínima:" . ($b->main->temp_min - 273) . "</p>";
            }
        }
        //FIN DEL CÓDIGO WIDGET
        echo $after_widget;
    }

}

add_action('widgets_init', create_function('', 'return register_widget("EltiempoWidget");'));
?>

Ejemplo shortcode con formulario


add_shortcode('tabla_multiplicar', 'trifulcas_tabla');

function trifulcas_tabla() {
?>
<form method = "post">
<p>Introduzca el número</p>
<input type="text" name="numero">
<input type="submit" name="enviar">
</form>
<?php
if (isset($_POST['enviar'])) {
$num = filter_input(INPUT_POST, 'numero');
?>
<table>
<?php
for ($i = 1; $i <= 10; $i++) {
?>
<tr><td><?= $i ?></td><td>x</td><td><?= $num ?></td><td>=</td><td><?= $i * $num ?></td></tr>
<?php
}
?>
</table>
<?php
}
}