<?php
/*
Plugin Name: WP Panda Quotes
Author: WP Panda
Version: 1.0
Author URI: https://wp-panda.com/
*/
/**
* Массив цитат
*
* @return array
*/
function cr_quotes_array() {
$out = array(
'author1' => array(
'Quisque dictum nunc et tempus placerat.',
'Cras non mi interdum, vestibulum massa eget, dapibus eros.',
'Donec sit amet dolor aliquam, euismod nunc eget, dictum neque.',
'Aliquam accumsan leo sed lorem porta, sit amet tempor nunc consequat.',
'Sed vitae massa nec velit molestie molestie ut ut velit.',
'In dignissim tortor id metus maximus finibus.',
'Sed dictum lectus ac facilisis rutrum.',
'Nullam non nibh ornare, porta massa sed, congue felis.'
),
'author2' => array(
'Nam eleifend felis nec urna consequat, quis vehicula metus porttitor.',
'Aliquam congue nulla non massa commodo sollicitudin.',
'Praesent ac felis vel sem dapibus consequat et quis lectus.',
'Donec iaculis neque quis purus eleifend, et fringilla nibh dapibus',
'Sed vitae massa nec velit molestie molestie ut ut velit.',
'In dignissim tortor id metus maximus finibus.',
'Sed dictum lectus ac facilisis rutrum.',
'Nullam non nibh ornare, porta massa sed, congue felis.'
),
'author3' => array(
'Sed vitae massa nec velit molestie molestie ut ut velit.',
'In dignissim tortor id metus maximus finibus.',
'Sed dictum lectus ac facilisis rutrum.',
'Nullam non nibh ornare, porta massa sed, congue felis.',
'Nam eleifend felis nec urna consequat, quis vehicula metus porttitor.',
'Aliquam congue nulla non massa commodo sollicitudin.',
'Praesent ac felis vel sem dapibus consequat et quis lectus.',
'Donec iaculis neque quis purus eleifend, et fringilla nibh dapibus',
),
'author4' => array(
'Sed a ex gravida, pharetra lacus sollicitudin, pulvinar erat.',
'Etiam sed metus vulputate, iaculis orci vel, accumsan purus.',
'Fusce congue quam et pharetra fermentum.',
'Nunc vel orci a ante vulputate facilisis.',
'Sed vitae massa nec velit molestie molestie ut ut velit.',
'In dignissim tortor id metus maximus finibus.',
'Sed dictum lectus ac facilisis rutrum.',
'Nullam non nibh ornare, porta massa sed, congue felis.'
)
);
return $out;
}
/**
* Вывод цитаты
* @param null $author - Автор
*
* @return string
*/
function cr_get_quotes_buttons( $author = null ) {
$array = cr_quotes_array();
$author = ! empty ( $author ) ? $author : array_rand( $array );
$out = '<blockquote>' . $array[ $author ][ array_rand( $array[ $author ] ) ] . '</blockquote>';
$out .= '<span>' . $author . '</span>';
$out .= '<p><input class="cr_words" type="button" value="Обновить" data-author="' . $author . '"><input class="cr_words" type="button" value="Новое"></p>';
return $out;
}
/**
* Добавление блока цитата до контента
* @param $content
*
* @return string
*/
function filtred_content( $content ) {
if ( ! is_singular() ) {
return $content;
} else {
return '<div class="cr-appender">' . cr_get_quotes_buttons() . '</div>' . $content;
}
}
add_filter( 'the_content', 'filtred_content' );
/**
* Скрипт для цитат
*/
function words_js() {
wp_enqueue_script( 'my_words', plugins_url( '/my_words.js', __FILE__ ), array( 'jquery' ), '1.0.0', true );
wp_localize_script( 'my_words', 'AjaxWords', array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'security' => wp_create_nonce( 'cr-word-string' )
) );
}
add_action( 'wp_enqueue_scripts', 'words_js' );
/**
* Аякс обработчик цитат
*/
function cr_word_action_callback() {
check_ajax_referer( 'cr-word-string', 'security' );
$author = ! empty( $_POST[ 'author' ] ) ? esc_html( $_POST[ 'author' ] ) : '';
echo cr_get_quotes_buttons( $author );
wp_die();
}
add_action( 'wp_ajax_cr_word_action', 'cr_word_action_callback' );
add_action( 'wp_ajax_nopriv_cr_word_action', 'cr_word_action_callback' );
jQuery(document).ready(function($) {
$(document).on('click','.cr_words',function(e) {
e.preventDefault();
var $data = {
action: 'cr_word_action',
security: AjaxWords.security,
author: $(this).data('author') ? $(this).data('author') : ''
};
$.post(AjaxWords.ajaxurl, $data, function ($response) {
$('.cr-appender').html($response);
});
});
});
<?php
$array = array(
's1','s2','s3','s4','d1','d2','d3','d4','t1','t2','t3','t4'
);
$n = 0;
echo '<div>';
foreach($array as $item ) {
if( $n%4 === 0 && $n !== 0 ) {
echo '</div><div>';
}
echo $item . ' ';
$n++;
}
echo '</div>';
$post_id
(int) (required) The post ID the media is associated with. If you don't want this media attached to a specific post, you can pass 0.
Сам код у вас непонятный, он поломан, оборачивайте его тегами <code>