Bei manchen Inhalten mag es interessant sein zu sehen, wann sie zuletzt aktualisiert worden sind. AGB, Cookie Privacy, Datenschutz oder auch Beiträge, die fortlaufend ergänzt werden, geben mit diesem Shortcode das Datum wieder an dem die letzte Änderung vorgenommen wurde.
Shortcode-Attribute
Das Zeitformat ist bestimmbar, so dass vom Jahr der Aktualisierung bis zum Zeitpunkt der Änderung jede gewünschte Granulierung darstellbar ist.
Optional umgeben before und after die Datumsangabe. Bei Angabe von tag wird der String in das entsprechende HTML-Element gepackt (vorausgesetzt es handelt sich um ein valid_tag
).
Plugin Code
<?php
/*
* Plugin Name: FLXO Last Modified Shortcode
* Plugin URI: https://webentwicklerin.at/
* Description: A Shortcode to display the date / time when a post was last modified. Usage: <strong>[last_mod format="l, j. F, Y" before="Last modified:" after="(by me)" tag="strong"]</strong> will show: <strong>Last modified: Friday, 23. April, 2018 (by me)</strong> <a href="http://php.net/manual/de/function.date.php" target="_blank" rel="noopener">PHP Date Formats</a>
* Version: 1.0.0
* Author: Gabriele Laesser
* Author URI: https://blog.webentwicklerin.at/
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
/* als Snippet, z.B. in der functions.php, erst ab hier kopieren */
class FLXO_Last_Modified {
function __construct() {
add_shortcode( 'last_mod', array( $this, 'flxo_last_modified_shortcode' ) );
}
function valid_tags() {
return array(
'strong',
'div',
'em',
'span',
'p',
'sup',
'sub'
);
}
function flxo_last_modified_shortcode( $atts ) {
/*
* to use date setting if format att is empty
*/
$date_format = get_option('date_format');
$atts = shortcode_atts( array(
'format' => $date_format,
'before' => '',
'after' => '',
'tag' => ''
), $atts );
$valid_tags = $this-> valid_tags();
$format = $atts['format'];
$before = ( !empty( $atts['before'] ) ? $atts['before'] . ' ' : '' );
$after = ( !empty( $atts['after'] ) ? ' ' . $atts['after'] : '' );
$open = ( !empty( $atts['tag'] ) && in_array( $atts['tag'], $valid_tags ) ? '<' . esc_attr( $atts['tag'] ) . '>' : '' );
$close = ( !empty( $atts['tag'] ) && in_array( $atts['tag'], $valid_tags ) ? '</' . esc_attr( $atts['tag'] ) . '>' : '' );
return $open . the_modified_date( $format, $before, $after, false ) . $close;
}
}
$flxo_last_modified = new FLXO_Last_Modified();
Schreibe einen Kommentar