Wordpress plugin uninstall.php - 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);

Related

Include post title wordpress in replace SQL

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' );
?>

How can I list all editors of a WordPress page, including those who edited (saved page) without creating a proper revision?

I'm trying to display a list of the people who have authored AND edited a page in Wordpress, not just the people who have authored revisions. the_modified_author() gives me the name of the person who last saved the page. birgire suggested this code to get the list of all editors, but it seems to only show the authors of the original post and all revisions, and doesn't include the names of those who saved a change that wasn't actually a revision:
function get_the_modified_authors_wpse_99226(){
global $wpdb;
$authors = array();
$results = $wpdb->get_results( $wpdb->prepare("SELECT post_author FROM $wpdb->posts WHERE (post_type = '%s' AND ID = %d) OR (post_type = 'revision' AND post_parent = %d) GROUP BY post_author", get_post_type( get_the_ID() ), get_the_ID(), get_the_ID() ) );
foreach($results as $row){
$authors[] = get_the_author_meta('display_name', $row->post_author );
}
return implode(", ", $authors);
}
Sometimes people edit a page (for example, they make an edit to a custom field) and then update the post. When that happens, they would be listed under the_modified_author() but would not be considered an author of a revision.
What I need is a combined list of both revision authors AND those people who have edited (saved) the page without creating a revision. Sort of like the function for modified_author but without the '_edit_last', true part.
Is this possible?
Please try the below code :
$revisions = $wpdb->get_results("select * from {$wpdb->posts} where post_parent={$post_id} and post_type='revision'")
You may need to do some change according to your need.

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().

Need some PHP help for making a change to wordpress plugin Groups

I'm really in a jam here. I am extremely close to launching my wordpress site. I have one rather large obstacle in my way that should be an easy fix. I am beginner level php so I am really struggling on this and any help or direction would be extremely appreciated since I have been trying to solve this issue for two weeks through reading and research.
Here is the issue and I will try to be as detailed as possible:
I have a custom post type called Portfolio.
I have a Portfolio page where it loads all of the featured images of
the Portfolio Posts into a grid on the Portfolio Page. You know a
basic portfolio setup where if you click the image it takes you to
the portfolio post.
Using the Groups plugin, when I restrict access to a Portfolio Post,
it removes the featured image from the Portfolio Page grid. Like the
post doesn't exist for non group members.
I am trying to sell access to these Portfolio Posts so if someone is
not a member they will not know there is anything there to buy.
I don't know how to make it to where those featured images still show on the Portfolio Page. I need people to know they are there but not be able to access them. As I mentioned in the support question, I cannot use a shortcode for this because the video is hard coded in to my theme. I need to add it to the code in a custom plugin to change groups to not remove that featured image from my portfolio page regardless of the restriction on the post.
I have gone through the API and found the section that controls access to the restricted posts. The Class is Groups-Post-Access. When both of these are removed from the class-groups-access.php I am able to see the featured images on the Portfolio page but it also removes the function that restricts the actual post. I don't know how I could tell the plugin to only return the featured image but still hide access to the post.
add_filter( 'posts_where', array( __CLASS__, 'posts_where' ), 10, 2 );
/**
* Filters out posts that the user should not be able to access.
*
* #param string $where current where conditions
* #param WP_Query $query current query
* #return string modified $where
*/
public static function posts_where( $where, &$query ) {
global $wpdb;
$user_id = get_current_user_id();
// this only applies to logged in users
if ( $user_id ) {
// if administrators can override access, don't filter
if ( get_option( GROUPS_ADMINISTRATOR_ACCESS_OVERRIDE, GROUPS_ADMINISTRATOR_ACCESS_OVERRIDE_DEFAULT ) ) {
if ( user_can( $user_id, 'administrator' ) ) {
return $where;
}
}
}
// 1. Get all the capabilities that the user has, including those that are inherited:
$caps = array();
if ( $user = new Groups_User( $user_id ) ) {
$capabilities = $user->capabilities_deep;
if ( is_array( $capabilities ) ) {
foreach ( $capabilities as $capability ) {
$caps[] = "'". $capability . "'";
}
}
}
if ( count( $caps ) > 0 ) {
$caps = implode( ',', $caps );
} else {
$caps = '\'\'';
}
// 2. Filter the posts that require a capability that the user doesn't
// have, or in other words: exclude posts that the user must NOT access:
// The following is not correct in that it requires the user to have ALL capabilities:
// $where .= sprintf(
// " AND {$wpdb->posts}.ID NOT IN (SELECT DISTINCT ID FROM $wpdb->posts LEFT JOIN $wpdb->postmeta on {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id WHERE {$wpdb->postmeta}.meta_key = '%s' AND {$wpdb->postmeta}.meta_value NOT IN (%s) ) ",
// self::POSTMETA_PREFIX . self::READ_POST_CAPABILITY,
// $caps
// );
// This allows the user to access posts where the posts are not restricted or where
// the user has ANY of the capabilities:
$where .= sprintf(
" AND {$wpdb->posts}.ID IN " .
" ( " .
" SELECT ID FROM $wpdb->posts WHERE ID NOT IN ( SELECT post_id FROM $wpdb->postmeta WHERE {$wpdb->postmeta}.meta_key = '%s' ) " . // posts without access restriction
" UNION ALL " . // we don't care about duplicates here, just make it quick
" SELECT post_id AS ID FROM $wpdb->postmeta WHERE {$wpdb->postmeta}.meta_key = '%s' AND {$wpdb->postmeta}.meta_value IN (%s) " . // posts that require any capability the user has
" ) ",
self::POSTMETA_PREFIX . self::READ_POST_CAPABILITY,
self::POSTMETA_PREFIX . self::READ_POST_CAPABILITY,
$caps
);
return $where;
}
add_filter( 'the_posts', array( __CLASS__, "the_posts" ), 1, 2 );
/**
* Filter posts by access capability.
*
* #param array $posts list of posts
* #param WP_Query $query
*/
public static function the_posts( $posts, &$query ) {
$result = array();
$user_id = get_current_user_id();
foreach ( $posts as $post ) {
if ( self::user_can_read_post( $post->ID, $user_id ) ) {
$result[] = $post;
}
}
return $result;
}
If someone can give me a better understanding of what is happening here and maybe an example of what he is referring to by creating a template for my custom post type, I would really appreciate it. Thank you so much for your time!
This was solved by using wordpress to handle hiding a specific category vs changing the actual plugin code.

Issue with wordpress calling query to database

Hey guys so I am trying to access my database which is within wordpress to get some field values and I tried doing just straight up PDO style but it seems to not work so I went into wordpress codex and did their way to call things, but still no success!
CODE:
/***GET USERNAME***/
global $current_user;
get_currentuserinfo();
$accusername = $current_user->user_login ;
/******SEE IF FIRST TIME DISCOUNT CODE BEEN USED*******/
$wpdb->query(
$wpdb->prepare(
"
SELECT firsttime_discount
FROM $wpdb->users
WHERE user-login = %d
",
$accusername
)
);
/******CHECK IT******/
echo"working";
$wpdb->query('query');
echo"working";
if ($checkFTDiscount->firsttimediscount != 1){
$validFTDiscount = 1;
}
else{
$validFTDiscount = 0;
echo"wori";
}
So it's suppose to go in and see if the value for discount code is set to 1 in the wp_user area, and if not just set a value to 0.
Let me know if you have any other questions.
Before you can use $wpdb, you must declare global $wpdb;
global $current_user, $wpdb;
Should do the trick.

Categories