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 


Thursday, December 3, 2020

How to connect to Wordpress SQL using shortcuts

Problem:

Yo need to access to any database table connected to your wordpress or maybe an external one to extract any information and process it.

Solution:

Use this TestSQLand add is to your wordpress functions.php of "Code Snipper" plugin:

function TestSQL_Function( $atts ) {
$link = mysqli_connect("localhost", "user", "****", "DATABASENAME");
$sql = "SELECT * FROM table";
if($result = mysqli_query($link, $sql)){
    if(mysqli_num_rows($result) > 0){
        while($row = mysqli_fetch_array($result)){
            $line = $line.$row['fielname1']."(".$row['fieldname2'].")";
        }
return $line;
        // Free result set
        mysqli_free_result($result);
    } else{
        return "No records matching your query were found.";
    }
} else{
    return "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
}
add_shortcode( 'TestSQL', 'TestSQL_Function' );

Now, you only need to add the wordpress shortcut [TestSQL] in your wordpres post or page.