Include post title wordpress in replace SQL - php

I need something similar to this:
$titulo = get_the_title( $post_id );
update wp_posts set post_content =
replace(post_content,'>Episódio','>$titulo Episódio');
I know that this function is wrong, but it is a way of exemplifying what I need.
I need to get the title wordpress and use with the SQL replace function

Backup your database first before trying this. Add this to your theme's 'functions.php', run only once and delete. Please note that this will replace all occurrences of 'Episodio' with '[post title] Episodio'. Keep FTP in hand since you may not able to access 'functions.php' before deleting this code.
<?php
function replace_string()
{
global $wpdb;
$table_name = $wpdb->prefix . "posts";
$results = $wpdb->get_results ( "SELECT * FROM $table_name ORDER BY ID" );
foreach ( $results as $postobj )
{
$post_content = str_replace( 'Episodio', $postobj->post_title.' Episodio', $postobj->post_content);
$selectd = "UPDATE {$table_name} SET `post_content` = '".$post_content."' WHERE ID='".$postobj->ID."'";
$wpdb->get_results($selectd);
}
die();
}
add_action( 'after_setup_theme', 'replace_string' );
?>

Related

Simple shortcode woes, coding for dummies

been asked to help a friend display some custom data on a Wordpress site, easy I thought...
Have a table with customer data in. Idea a plugin to is to query the table based on current user id and spit it out as shortcodes so they can place the output where ever they like.
After much googling (I'm not a coder) I got something that worked! Problem is, even to my untrained eye, it looks like a bodge, repeating queries when there's clearly a more intelligent way of doing it.... Can someone clever please show me the way based on the this >> ?
<?php
Plugin Name: do stuff
Description: Do site specific stuff
function spend_data() {
global $wpdb;
global $current_user;
wp_get_current_user();
$userid = $current_user->ID;
$spend = $wpdb->get_var ( "SELECT spend FROM mytable WHERE the_id=$userid");
return $spend;
}
function detail_data() {
global $wpdb;
global $current_user;
wp_get_current_user();
$userid = $current_user->ID;
$detail = $wpdb->get_var ( "SELECT detail FROM mytable WHERE the_id=$userid");
return $programs;
}
add_shortcode('spend', 'spend_data');
add_shortcode('detail', 'detail_data');
/* Stop Adding Functions Below this Line */
I guess you could do a query for all fields at a hook that fires before the shortcodes and then store it all in $current_user. Don't forget to replace mytable with the correct tablename.
function marks_prepare_user_shortcodes () {
global $wpdb;
global $current_user;
$current_user = wp_get_current_user();
$userid = $current_user->ID;
$row = $wpdb->get_row( "SELECT * FROM mytable WHERE the_id=".$userid, ARRAY_A);
$current_user->marks_shortcodes = $row;
}
add_action( 'wp_head', 'marks_prepare_user_shortcodes');
And then your shortcodes change to
function spend_data() {
global $current_user;
$spend = $current_user->marks_shortcodes['spend'];
if (!is_null($spend))
return $spend;
}
function detail_data() {
global $current_user;
$detail = $current_user->marks_shortcodes['detail'];
if (!is_null($detail))
return $detail;
}
add_shortcode('spend', 'spend_data');
add_shortcode('detail', 'detail_data');
I'm not sure if wp_head is the correct /best action to pick here, you could give it a try. I figured it is called before all the shortcodes are. Code is untested.
Ps. As a sidenote, I believe your extra user data should actually be stored in the wp_usermeta table, instead of a separate table. You'd retrieve the fields with get_user_meta(): https://developer.wordpress.org/reference/functions/get_user_meta/
Update
Instead of the code block directly above, you could also use this:
function mikes_data_shortcode($atts) {
global $current_user;
$atts = array_change_key_case((array)$atts, CASE_LOWER);
// override default attributes with user attributes
$actual_atts = shortcode_atts([
'field' => 'YouDidntEnterAFieldNameAsAttribute',
], $atts);
$data = $current_user->marks_shortcodes[$actual_atts['field']];
if (!is_null($data))
return $data;
}
function mikes_shortcodes_init()
{
add_shortcode('mikes_data_shortcode', 'mikes_data_shortcode');
}
add_action('init', 'mikes_shortcodes_init');
Where you use shortcodes like this:
[mikes_data_shortcode field="spend"]
or
[mikes_data_shortcode field="detail"]

Wordpress plugin uninstall.php

I am trying to write a simple plugin that, among other things, creates a custom post type.
I have been searching everywhere but I have really no idea on how to write the uninstall function that delete the custom post type and all its entries from the db.
I have found this on the WordPress codex, but I don't understand what to do with it.. shame
if ( !defined( 'WP_UNINSTALL_PLUGIN' ) )
exit();
$option_name = 'plugin_option_name';
delete_option( $option_name );
// For site options in multisite
delete_site_option( $option_name );
//drop a custom db table
global $wpdb;
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}mytable" );
Can anyone help?
Thanks in advance
Would this be correct?
if ( !defined( 'WP_UNINSTALL_PLUGIN' ) )
exit();
global $wpdb;
$posts_table = $wpdb->posts;
$query = "
DELETE FROM wp_posts
WHERE post_type = 'messages'
";
$wpdb->query($query);
The WP codex page http://codex.wordpress.org/Function_Reference/register_uninstall_hook has 2 important pieces of information, only 1 of which you list in your question. You do need to make sure that you register the hook.
That aside, if you want to remove all custom post data (regardless if it is upon uninstallation or as another person commented having a seperate button to remove data as many plugins do) you need to be sure to remove the postmeta records as well.
global $wpdb;
$cptName = 'messages';
$tablePostMeta = $wpdb->prefix . 'postmeta';
$tablePosts = $wpdb->prefix . 'posts';
$postMetaDeleteQuery = "DELETE FROM $tablePostMeta".
" WHERE post_id IN".
" (SELECT id FROM $tablePosts WHERE post_type='$cptName'";
$postDeleteQuery = "DELETE FROM $tablePosts WHERE post_type='$cptName'";
$wpdb->query($postMetaDeleteQuery);
$wpdb->query($postDeleteQuery);

register_uninstall_hook not working on wordpress plugin uninstall

Hi there i make a plugin, now i want that when a user deactivate the plugin, all the functions and posts and option values and meta values also be deleted.
I write a hook and make a file where i delete all the values, but it did not work, i use this hook
register_uninstall_hook('foo_uninstall.php', $callback);
Is there any other hook to use for this purpose.
Here is my foo_uninstall.php code
<?php
global $wpdb;
$del_prefix = $wpdb->prefix;
//if uninstall not called from WordPress exit
if(!defined('WP_UNINSTALL_PLUGIN'))
exit();
//=========> Delete foo Options
delete_option('foo_theme_directory');
delete_option('foo_plugin_slug');
delete_option('foo_article_qty');
delete_option('foo_search_setting');
delete_option('foo_breadcrumbs_setting');
delete_option('foo_sidebar_setting');
delete_option('foo_comments_setting');
delete_option('foo_bgcolor');
delete_option('widget_foo_article_widget');
delete_option('foo_cat_children');
//=========> Get and Delete Foo Page
$foo_get_page = $wpdb->get_results("SELECT * FROM ".$del_prefix."posts WHERE post_type='page' And
post_content = '[foo_article]' And post_name = 'article'",ARRAY_A );
$fooPageID = $foo_get_page['ID'];
$wpdb->query("DELETE FROM ".$del_prefix."postmeta WHERE post_id = '$fooPageID'");
$wpdb->query("DELETE FROM ".$del_prefix."posts WHERE post_title Like '%article%' And post_type =
'page'");
//=========> Delete foo Terms and Taxonomies
$foo_Find_tax = $wpdb->get_results("SELECT term_taxonomy_id FROM ".$del_prefix."term_taxonomy WHERE
taxonomy='foo_article'",ARRAY_A );
$fooTaxID = $foo_Find_tax[0]['term_taxonomy_id'];
foreach($foo_Find_tax as $foo_find_tax){
$fooTaxID = $foo_find_tax['term_taxonomy_id'];
$delete_foo_relations = $wpdb->query("DELETE FROM ".$del_prefix."term_relationships WHERE
term_taxonomy_id='$fooTaxID'");
$delete_foo_tax = $wpdb->query("DELETE FROM ".$del_prefix."term_taxonomy WHERE
term_taxonomy_id='$fooTaxID'");
$delete_foo_terms = $wpdb->query("DELETE FROM ".$del_prefix."terms WHERE term_id='$fooTaxID'");
}
//=========> Delete terms_order From wp_terms
$wpdb->query("ALTER TABLE ".$del_prefix."terms DROP `terms_order`");
//=========> Delete comments of articles
$foo_comments = $wpdb->get_results("SELECT ID FROM ".$del_prefix."posts WHERE post_type =
'foo_article'");
$foo_comment_id = $foo_comments->ID;
foreach($foo_comments as $foo_comment){
$foo_comment_id = $foo_comment->ID;
$wpdb->query("DELETE FROM ".$del_prefix."comments WHERE comment_post_ID='$foo_comment_id'");
}
//=========> Delete All Articles and Attachments
$foo_all_articles = new WP_Query('post_type=foo_article&posts_per_page=-1');
if($foo_all_articles){
while($foo_all_articles->have_posts()) :
$foo_all_articles->the_post();
$fooID = get_the_id();
$foo_del_args = array('post_parent' => $fooID);
$foo_attachments = get_children($foo_del_args);
if($foo_attachments){
foreach($foo_attachments as $foo_attachment){
wp_delete_attachment($foo_attachment->ID, true);
}
}
wp_delete_post($fooID, true);
endwhile;
}
//=========> Remove all files and images
unlink(get_template_directory()."/foo_articles.php");
unlink(get_template_directory()."/foo_style.css");
unlink(get_template_directory()."/single-foo_articles.php");
unlink(get_template_directory()."/taxonomy-foo_cat.php");
unlink(get_template_directory()."/foo_search.php");
?>
try register deactivation hook instead of register uninstall hook . Please do refer here for more.
register_uninstall_hook( __FILE__, 'foo_on_uninstall' );
function foo_on_uninstall() {
// write your uninstall code
}
Remove the part
if(!defined('WP_UNINSTALL_PLUGIN'))
exit();
According to the Plugin Handbook, WP_UNINSTALL_PLUGIN is not defined when uninstall is performed by register_uninstall_hook().

Wordpress query posts by title issue

I'm using below code to get posts by title.
protected function get_post_by_title($post_title, $output = OBJECT, $post_type = 'post' )
{
global $wpdb;
$post = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type= %s", $post_title, $post_type ));
if ( $post )
return get_page($post, $output);
return null;
}
Everything is working fine, except it will not find the posts having single quote in title. Consider below as $post_title.
This is a test's post
$wpdb->prepare will return query something like below.
This is a test\\\'s post
Which will return no result. Can anyone please help me on this ?
Thanks in advance
You should never compare with the real title. Wordpress offers you the possibility to create slugs without all those weird characters like " " or "'". Then you can compare them:
Use sanitize_title() to create a slug from your title, and then you can compare them.
I think this should fix the issue,
DOC: http://in1.php.net/manual/en/mysqli.real-escape-string.php
protected function get_post_by_title($post_title, $output = OBJECT, $post_type = 'post' )
{
global $wpdb;
$post_title = mysqli_real_escape_string($post_title); //escape special meaning
$post = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type= %s", $post_title, $post_type ));
if ( $post )
return get_page($post, $output);
return null;
}
edit:
That might not work, try this,
$post_title = addslashes($post_title);
Let me know which one works for you.

How to get the booking ID where current page ID in Wordpress

What php code can be used to retrieve the current page ID in a Wordpress theme? Then pull the booking_type_id from the database that is linked to that ID.
I have tried so many different things and I just cant seem to get it to work.
I am trying to use something along the lines of
<?php
$bookingid = $wpdb->get_var("SELECT booking_type_id FROM hobookingtypes WHERE post_id = 'the_ID();' ");
// Echo the booking resource id
echo $bookingid;
?>
But I am really not sure what I am doing wrong.
You can't use the_ID() because its echo the POST_ID, so use the global object $post the provide all the information related to post;
global $post;
$bookingid = $wpdb->get_var(
"SELECT booking_type_id FROM hobookingtypes
WHERE
post_id = '". $post->ID ."'"
);
// Echo the booking resource id
echo $bookingid;
Alternative of global $post:
OR you can use get_the_ID() instead of the_ID()
You can get current post ID with this code,
global $post;
$id = $post->ID;
or just,
global $post_id;
$id = $post_id;
or you can use,
get_the_ID() function insted.

Categories