Calcetines Extreme

Calcetines Extreme
Take care of you using the best socks

Friday, December 25, 2020

How to text spin using a wordpress shorcut

Problem:
You need to create random text but with sense, for example, you have a web page that have thousand of pages and you would like to create text automatically using text spin technique.

Solution:
1.- Create a new function inside your Wordpress "Snippets" plugin.

function fun_textspin() {
class Spintax
{
public function process($text)
{
return preg_replace_callback(
'/\{(((?>[^\{\}]+)|(?R))*?)\}/x',
array($this, 'replace'),
$text
);
}
public function replace($text)
{
$text = $this->process($text[1]);
$parts = explode('|', $text);
return $parts[array_rand($parts)];
}
}
$spintax = new Spintax();
$string = '{Hello|Howdy|Hola} to you, {Mr.|Mrs.|Ms.} Smith|Williams|Davis}!';
return $spintax->process($string);
}

add_shortcode( 'sc_textspin','fun_textspin' );


 

2.- add the following Shorcut to your wordpress page.

<!-- wp:paragraph -->

<p>[sc_textspin]</p>

<!-- /wp:paragraph -->


Source:
https://gist.github.com/irazasyed/11256369