Jump to content

Writing a Query for Wordpress


transfield

Recommended Posts

Hi,
I have a Wordpress plugin which I want to edit. My code is below. I have made comments in the areas that I need help in coding. I would appreciate your assistance in showing me the correct way to write the code.

 

Thanks

<?php 
global $wpdb; //this should be correct 

//I need help to convert the 2 lines below to a Wordpress friendly query 
$query1="SELECT * FROM wp_users WHERE user_login LIKE '%z%'"; 
$result1 = mysql_query($query1); 

//I want to put the search results into the example below 
if ( $user_login == 'admin' || $user_login == 'joe' ) { 
$do_something;
 } 
?>
Link to comment
Share on other sites

Surely that is not your entire plugin.

 

Did you check out the wordpress codex about plugins?

 

If you really wanted to create your own plugin for this the wp queries get complex and a few joins on the tables.

 

Here is just an example of a plugin that will show only if is admin or network superadmin

<?php
/*
Plugin Name: I Am Admin
Plugin URI: http://site.com
Description: Shows a message are admin
Author: admin@site.com
Version: 1.0
Author URI: http://site.com
*/

function i_am_admin(){

if(current_user_can('manage_options') || is_super_admin()) {

	echo "

<style type='text/css'>

#admin_panel {
top: auto !important;
bottom: 0;
}

p.admin_panel {
top: -20;
width: 100%;
background-color:white;
color: red;
border-style:solid;
border-width:1px;
text-decoration:none;
text-shadow:1px 1px 0px #000000;
text-align:center;
margin-top:40px;
margin-bottom:0px;
margin-right: auto;
margin-left: auto;
padding-top: 20px;
padding:0px;
}

</STYLE>";
echo "<div><p class='admin_panel'>I am The Admin</p></div>";

}

}

add_action('wp_head', 'i_am_admin');

?>

As for the search queries you can download a search plugin and examine what they used.(or use theirs and wrap it with the check for admin)

https://wordpress.org/plugins/wp-user-listing-and-searching/

Link to comment
Share on other sites

I've figured out the 1st part which is:

<?php
global $wpdb;
$my_table = $wpdb->prefix."users";
$my_query = "Select user_login from $my_table where user_login LIKE '%z%' ";
$my_results = $wpdb->get_results($my_query);
?>

Now, in second part, how do I put the search results into the foreach loop? Lets assume that I have 100 records in the search results. How do I loop through all the 100 records using the code below:

<?php
foreach($my_results as $user_data)
{
if($user_data -> user_login === 'admin' || $user_data -> user_login === 'joe' //all hundred records need to go inside here. How do I do that?)
// do something
}
?>

Thanks for your help.

Link to comment
Share on other sites

Are you looking to echo the name?

 

You say want 100 results but the if statement would only show for admin or joe.

foreach($my_results as $user_data){

    if($user_data -> user_login === 'admin' || $user_data -> user_login === 'joe'){
        echo "Admin<br />";
    }else{
        echo $user_data->name."<br />";
    }

}
Edited by QuickOldCar
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.