Wordpress hook write to database before post save - wordpress

I am beginner in wordpress and i would like to ask some help.
I need some hook , which will split the post after some number of characters (lets say 300) and will add my text after each split.I created filter hook, but i do not know how to create action hook - to write splitted text with my text to database before save,publish or edit action. This code works , but how to save to database?
function inject_ad_text_after_n_chars($content) {
$enable_length = 30;
global $_POST;
if (is_single() && strlen($content) > $enable_length) {
$before_content = substr($content, 0, 30);
$before_content2 = substr($content, 30, 30);
$before_content3 = substr($content, 60, 30);
$before_content4 = substr($content, 90, 30);
$texta = '00000000000000000000';
return $before_content . $texta . $before_content2 . $texta . $before_content3 . $texta . $before_content4;
}
else {
return $content;
}
}
add_filter('the_content', 'inject_ad_text_after_n_chars');

You are looking for wp_insert_post_data filter. In this function, to parameter is supplied for you to filter the data before it is inserted. You can use it like -
add_filter('wp_insert_post_data', 'mycustom_data_filter', 10, 2);
function mycustom_data_filter($filtered_data, $raw_data){
// do something with the data, ex
// For multiple occurence
$chunks = str_split($filtered_data['post_content'], 300);
$new_content = join('BREAK HERE', $chunks);
// For single occurence
$first_content = substr($filtered_data['post_content'], 0, 300);
$rest_content = substr($filtered_data['post_content'], 300);
$new_content = $first_content . 'BREAK HERE' . $rest_content;
// put into the data
$filtered_data['post_content'] = $new_content;
return $filtered_data;
}

Related

how to add three dots after excerpt in woocommerce products?

I have used this code to limit the excerpts. But I need to add three dots after limiting the word count
add_filter('woocommerce_short_description', 'limit_woocommerce_short_description', 10, 1);
function limit_woocommerce_short_description($post_excerpt){
if (!is_product()) {
$pieces = explode(" ", $post_excerpt);
$post_excerpt = implode(" ", array_splice($pieces, 0, 20));
}
return $post_excerpt;
}
code is taken from https://stackoverflow.com/a/38472433

PHP SQLite3 query not displaying all rows

I have a PHP script show below that queries for all rows. The result set only shows one row even though there are more.
mbrlist.php
`<?php
$html = "";
class MyDB extends SQLite3
{
function __construct()
{
$this->open('privdata/helpseekers.db');
}
}
$db = new MyDB();
if ( !$db )
{
echo $db->lastErrorMsg();
}
else
{
$rows = $db->query("SELECT COUNT(id) as count FROM accounts");
$row = $rows->fetchArray(SQLITE3_ASSOC);
$numRows = $row['count'];
echo "Opened database successfully - " . $numRows . " Accounts Found\n";
}
$ret = $db->query("SELECT id,userid,fname,lname,password,email,userrole FROM accounts ORDER BY id ASC");
while ( $rec = $ret->fetchArray() )
{
$html = " <td>{$rec[0]}</td> <td>{$rec[1]}</td> <td>{$rec[2]}</td> <td>{$rec[3]}</td> <td>{$rec[4]}</td> <td>{$rec[5]}</td> <td>{$rec[6]}</td> </tr>\n";
if ( !$rec = $ret->fetchArray() ) break;
}
$db->close();
echo $html;
?>`
So why does it not return more than one row?
In the loop you do :
// Before loop variable html contains ""
// on next line you set into $rec the next row data
// in case there is no more row you exit or not not enter the loop
while ( $rec = $ret->fetchArray() )
{
// anything that was inside $html before this assignment is replaced
// by <td>...</td> of current row
$html = " <td>{$rec[0]}</td> <td>{$rec[1]}</td> <td>{$rec[2]}</td> <td>{$rec[3]}</td> <td>{$rec[4]}</td> <td>{$rec[5]}</td> <td>{$rec[6]}</td> </tr>\n";
// here you get the next row in $rec and test if it is not null
// if it is null you exit the loop
if ( !$rec = $ret->fetchArray() ) break;
// at the end of the loop, the code in while ($rec = ...) is
// executed to get row of next data
}
$db->close();
// here you send the contents of $html to client browser
echo $html;

WordPress Nested Second Add Action is not working

Second add_action function is not working but I am not getting any error. if I take the second add_action out of the function it works outside but since I want it to work on specific pages I can't work with that.
Do you have any suggestion how may I fix this problem?
add_action('current_screen', function ($current_screen) {
if ('smart_board' == $current_screen->post_type && 'post' == $current_screen->base) {
add_action('add_attachment', function ($post_ID) {
$file = get_attached_file($post_ID);
$path = pathinfo($file);
$file_slugified_name = $path['filename'];
$file_prefix = 'smart_board_';
$file_dir = $path['dirname'];
$file_extension = $path['extension'];
$newfilename = $file_prefix . $file_slugified_name;
$newfile = $file_dir . "/" . $newfilename . "." . $file_extension;
rename($file, $newfile);
update_attached_file($post_ID, $newfile);
});
}
});

Force auto replace letters when typing inside order form

Does anyone have an idea on how to auto replace letters in woocommerce order forms! basically I need when someone is typing in greeklish to auto replace those letters to Greek!
I think you will have to use this hook to get the newly created order :
add_action( 'woocommerce_new_order', 'convert_greeklish_for_wc_order', 1, 1 );
function create_invoice_for_wc_order() {
function create_invoice_for_wc_order( $order_id ) {
// get order details data...
$order = new WC_Order( $order_id );
// Here goes the code to get all the fields
// Convert fields to greek
// Set new fields values
};
}
And adapt this code to convert greeklisk to greek:
<?php
function greeklish($new_text){
$greek_len = array('α','ά','Ά','Α','β','Β','γ', 'Γ', 'δ','Δ','ε','έ','Ε','Έ','ζ','Ζ','η','ή','Η','θ','Θ','ι','ί','ϊ','ΐ','Ι','Ί', 'κ','Κ','λ','Λ','μ','Μ','ν','Ν','ξ','Ξ','ο','ό','Ο','Ό','π','Π','ρ','Ρ','σ','ς', 'Σ','τ','Τ','υ','ύ','Υ','Ύ','φ','Φ','χ','Χ','ψ','Ψ','ω','ώ','Ω','Ώ',' ',"'","'",',');
$english_len = array('a', 'a','A','A','b','B','g','G','d','D','e','e','E','E','z','Z','i','i','I','th','Th', 'i','i','i','i','I','I','k','K','l','L','m','M','n','N','x','X','o','o','O','O','p','P' ,'r','R','s','s','S','t','T','u','u','Y','Y','f','F','ch','Ch','ps','Ps','o','o','O','O',' ','',' ',' ');
$new_text = str_replace($greek_len,$english_len,$new_text);
return $new_text;
}
$conv = greeklish("Το κείμενο σου εδώ!");
echo $conv; #To keimeno sou edo!
?>

Wordpress - Excerpt character alternative?

I'm totally new to WordPress so be easy :)
I the following code in a template:
<?php excerpt(20);?>
What this does is limit the text with 20 words. I am now wondering if there is some sort of similar function that limits by characters instead of words?
Thanks!
I use this:
add_filter('excerpt_length', 'my_excerpt_length');
function my_excerpt_length($length) {
return '500';
}
function better_excerpt($limit, $id = '') {
global $post;
if($id == '') $id = $post->ID;
else $id = $id;
$postinfo = get_post($id);
if($postinfo->post_excerpt != '')
$post_excerpt = $postinfo->post_excerpt;
else
$post_excerpt = $postinfo->post_content;
$myexcerpt = explode(' ', $post_excerpt, $limit);
if (count($myexcerpt) >= $limit) {
array_pop($myexcerpt);
$myexcerpt = implode(' ',$myexcerpt).'...';
} else {
$myexcerpt = implode(' ',$myexcerpt);
}
$myexcerpt = preg_replace('`\[[^\]]*\]`','',$myexcerpt);
$stripimages = preg_replace('/<img[^>]+\>/i', '', $myexcerpt);
return $stripimages;
}
And then in my theme file, I just call it in with:
better_excerpt('50') //50 being how many words I want
Useful for custom plugins/widgets too.
Wordpress doesn't support the character delimiter for the excerpt method, there's a plugin called Advanced Excerpt that does. After installing you can call the_advanced_excerpt('length=20&use_words=0')
I use this in my functions.php:
function truncate ($str, $length=10, $trailing='...'){
// take off chars for the trailing
$length-=mb_strlen($trailing);
if (mb_strlen($str)> $length){
// string exceeded length, truncate and add trailing dots
$str = mb_substr($str,0,$length);
$str = explode('. ',$str);
for( $i=0; $i<(sizeof($str)-2); $i++ ):
$newstr .= $str[$i].". ";
endfor;
return $newstr;
} else{
// string was already short enough, return the string
$res = $str;
}
return $res;
}
It should truncate to a character count, but then truncate back further to the last period before the truncation. It does get problematic when your excerpt includes links, however, or other markup - in other words, it's best to use the Excerpt field in the post rather than auto-excerpting with this function, because you can't use HTML in the excerpt field.
Please use this code for limiting post content...
<?php substr($post->post_content, 0, xy); ?> ...
Change the limit of XY....

Resources