wordpress - to find if a meta_value exists - wordpress

I'm working on a wordpress site and I have custom field 'mobile' in usermeta table. When a user register, I need to check if mobile number exists.I'm using zm-ajax-login-register plugin which doesnot have a mobile field. I'm successfull in adding field and value to usermeta,but I cant validate.
I tried like this,but metadata_exists is not the right code
if ( $user['mobile']!= ''){
$your_custom_field= metadata_exists( 'user', $user->ID, $user['mobile'] );
if($your_custom_field){
$status =$this->_zm_alr_helpers->status('invalid_mobile');
}
}

Hooray.....Finally I got the answer...
$args = array(
'meta_value' => $user['mobile']
);
$user_mobile_exists=get_users( $args );
if ($user_mobile_exists)
{
$status =$this->_zm_alr_helpers->status('invalid_mobile');
}

Related

Wordpress plugin for course registration

I'm building a WordPress Page for course registration. All I want the plugin to do is send the filled in form details to my email ID and send an email to the user that he/she has successfully registered for the course. I don't need users to signup with username and password.
I've tried my luck with WP Forms but it only seems to have the option to forward the email to me and not the user.
Any suggestion on which plugin I should use?
As #Hughes mentioned, you cant use wpcf7, and just hook on it to insert custom post on every query.
// Hook on wpcf7
add_filter( 'wpcf7_mail_components', 'do_on_cf7_submit', 50, 2 );
function do_on_cf7_submit($mail_params, $form = null) {
// Empty post content
$content = '';
// set post content if field not empty
if ($_POST['field-name'] != '') {
$content .= 'Field Name Label: '.$_POST['field-name'] ;
}
// insert post if content not epmty
if ($content != '') {
insertQueryPost($_POST['email'], $content);
}
// allow cf7 to do his stuff
return $mail_params;
}
// insert custom post type "query", don't forget to setup your custom post type first
function insertQueryPost($title, $content) {
// insted of proper post slug, just make a hashed slug, when setting custom post type, set it to not public and not search-able
$t = time();
$thash = md5($t);
$my_query = array(
'post_title' => wp_strip_all_tags( $title ),
'post_content' => $content,
'post_type' => 'query',
'post_name' => $thash,
'post_status' => 'publish',
'post_author' => 1
);
$data = wp_insert_post( $my_query );
}

Show only current user's posts in wordpress

I'm trying to show posts only that the current user has created on the frontend. That is, current, logged in user should be able to view only the posts he/she created across the website, including in the blog archive I want to display only current user's posts. And anyone with a link should be able to view the post too.
I tried the code below, but this also limits access to the pages which current user didn't create. And I need to limit access only to posts.
function exclude_other_users_posts( $query ) {
if( !is_user_logged_in() ) {
// guests cannot read private posts
// and we exclude all public posts here
// so guests can read nothing :-)
$query->set( 'post_status', 'private' );
$query->set( 'perm', 'readable' );
} elseif( !current_user_can('read_others_posts') )
$query->set( 'author', get_current_user_id() );
}
add_action( 'pre_get_posts', 'exclude_other_users_posts' );
You can use something like this in the php files that you want to display your posts.
global $current_user;
get_currentuserinfo();
$authorID = $current_user->ID;
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'author' => $authorID
);
$wp_query = new WP_Query($args);
Then you can make your loop to show your posts.

Wordpress Profile File / Page

I would like to create a file in the Wordpress theme, where i will add my own code, edit profile, show profile information, and perhaps an ability to insert posts / meta data programmatically.
So it needs to be www.mysite.com/profile.php or www.mysite.com/profile/
I do not want to use Buddy Press or any other plugin.
I know how the template system works, i do not want a page template.
It will probably be a class, later on, i do not want to change .htaccess file, and if i must i would appreciated filter function how to do this from functions.php
Basically just a simple .php file i can link to, located in theme root.
include('../../../wp-load.php');
and write any code i would like to.
Any creative solution that is not too "hacky" would be appreciated.
Spent around 2 days googling bashing my head on this, before i decided to ask question.
Thank you very much.
Ok, I managed to do this, took me 2 days to figure it out. Here is how I managed to do it:
Make a plugin folder.
In that plugin folder make 1x php file. so index.php
Ok so first thing we need to register plugin I did it like this, in your index.php paste
this code.
function activate_profile_plugin() {
add_option( 'Activated_Plugin', 'Plugin-Slug' );
/* activation code here */
}
register_activation_hook( __FILE__, 'activate_profile_plugin' );
Then we need a function when you register a plugin only once register profile pages.
function create_profile_page( $title, $slug, $post_type, $shortcode, $template = null ) {
//Check if the page with this name exists.
if(!get_page_by_title($title)) {
// if not :
$page_id = -1;
$page_id = wp_insert_post(
array(
'comment_status' => 'open',
'ping_status' => 'open',
'post_content' => $shortcode,
'post_author' => 1, // Administrator is creating the page
'post_title' => $title,
'post_name' => strtolower( $slug ),
'post_status' => 'publish',
'post_type' => strtolower( $post_type )
)
);
// If a template is specified in the function arguments, let's apply it
if( null != $template ) {
update_post_meta( get_the_ID(), '_wp_page_template', $template );
} // end if
return $page_id;
}
}
Ok so we created function which programatically register pages. It has 5 paramethers.
is Title
Slug
Post type
Shortcode.
Template
For the shortcode template you need to make a shortcode with the complete page output
and add it as a parameter to this function, so for registration page it will be a shortcode with the registration forms etc.
For example :
function registration_shortcode(){
echo 'Wellcome to Registration page';
}
add_shortcode('registration_output', 'registration_shortcode');
Next thing we need to call it once only when plugin loads.
so we do this :
function load_plugin() {
if ( is_admin() && get_option( 'Activated_Plugin' ) == 'Plugin-Slug' ) {
delete_option( 'Activated_Plugin' );
/* do stuff once right after activation */
// example: add_action( 'init', 'my_init_function' );
create_profile_page('Registration', 'registration', 'page', '[registration_output]');
create_profile_page('Profile', 'profile', 'page', '[profile_shortcode]');
create_profile_page('Profil Edit', 'profile-edit', 'page', '[edit_shortcode]');
}
}
add_action( 'admin_init', 'load_plugin' );
Ok so this will execute only once when plugin loads and it will create 3 Pages, which are Profile, Registration and Profile Edit.
And that's it, you have your front-end user profile blank pages, and you can write page output in shortcodes ,create more pages, put any forms or elements you like and create decent profile (which doesn't have any stuff you don't need in it like plugins. )
Hope this helps, it was painful for me to figure this out. Cheers!

Wordpress - Own plugin - get all images of the media gallery

i've written a plugin for the Wordpress TinyMCE editor and i want to use images which have been previously uploaded to the media gallery in that plugin. How can i access all uploaded pictures in my own plugin? I can't find it in the wordpress docs.
Thanks.
I am slightly not clear on your question. Are you looking for a method to access the Add Media button?
In attempts to answer your question -> This method allows you to get all attachments that is in your media section.. Currently it displays everything but you can manipulate it the way you want to.
Reference: http://codex.wordpress.org/Template_Tags/get_posts
$args = array( 'post_type' => 'attachment', 'numberposts' => -1);
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
echo apply_filters( 'the_title' , $attachment->post_title );
the_attachment_link( $attachment->ID , false );
}
}

wp_redirect function working locally, but not on server

I'm using code directly from this tutorial: http://voodoopress.com/how-to-post-from-your-front-end-with-no-plugin/. When set up locally the redirect works fine. When the exact same site is hosted on a rackspace cloud site, it doesn't. What's going on?
Notable piece:
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == "new_post") {
// Do some minor form validation to make sure there is content
if (isset ($_POST['title'])) {
$title = $_POST['title'];
} else {
echo 'Please enter the wine name';
}
if (isset ($_POST['description'])) {
$description = $_POST['description'];
} else {
echo 'Please enter some notes';
}
$tags = $_POST['post_tags'];
// ADD THE FORM INPUT TO $new_post ARRAY
$new_post = array(
'post_title' => $title,
'post_content' => $description,
'post_category' => array($_POST['cat']), // Usable for custom taxonomies too
'tags_input' => array($tags),
'post_status' => 'publish', // Choose: publish, preview, future, draft, etc.
'post_type' => 'post' //'post',page' or use a custom post type if you want to
);
//SAVE THE POST
$pid = wp_insert_post($new_post);
//SET OUR TAGS UP PROPERLY
wp_set_post_tags($pid, $_POST['post_tags']);
//REDIRECT TO THE NEW POST ON SAVE
$link = get_permalink( $pid );
wp_redirect( $link );
} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM
//POST THE POST YO
do_action('wp_insert_post', 'wp_insert_post');`
It's pretty straightforward. The code is posting a post using wp_insert_post and redirecting to that post's link afterward. All well and good, and it works on my XAMPP setup. But here's the issue: I can't get any permutation of this to work on a live server. I've started with fresh WP installs several times, only placing this code in a home.php, and to no avail. Any idea as to what the heck could be the problem?
Just a quick thought, is there any white space above the redirect in any of your pages? Redirects must be called before any white space or other elements are added to the page so if you are echoing something before your redirect it will not work.

Resources