jeudi 1 janvier 2015

Check If comment author is registered


It should be simple, but I tried some codes, none does the trick. Searched on Google, nothing there either. Simply check if the comment author is a registered user (so I can add some code for it), if it's not registered show nothing.


This should do the trick if( empty($comment_author_nickname) && empty($comment_author_email) ) but I don't know how to get that information


thank you





A data field is not inesrting into database table from custom registration form


My registration form working fine. All data inserting into database table but "About/Bio" field not inserting into database table. Please help me out



<?php

function registration_form( $username, $password, $email, $website, $bio ) {
echo '
<style>

div {
margin-bottom:2px;
}

input{
margin-bottom:4px;
}

</style>
';

echo '
<form action="' . $_SERVER['REQUEST_URI'] . '" method="post">
<div>
<label for="username">Username <strong>*</strong></label>
<input type="text" name="username" value="' . ( isset( $_POST['username'] ) ? $username : null ) . '">
</div>

<div>
<label for="password">Password <strong>*</strong></label>
<input type="password" name="password" value="' . ( isset( $_POST['password'] ) ? $password : null ) . '">
</div>

<div>
<label for="email">Email <strong>*</strong></label>
<input type="text" name="email" value="' . ( isset( $_POST['email']) ? $email : null ) . '">
</div>
<div>
<label for="email">Email <strong>*</strong></label>
<input type="text" name="confirm_email" value="' . ( isset( $_POST['confirm_email']) ? $confirm_email : null ) . '">
</div>

<div>
<label for="website">Website</label>
<input type="text" name="website" value="' . ( isset( $_POST['website']) ? $website : null ) . '">
</div>


</div>

<div>
<label for="bio">About / Bio</label>
<textarea name="bio">' . ( isset( $_POST['description']) ? $bio : null ) . '</textarea>
</div>
<input type="submit" name="submit" value="Register"/>
</form>


';
}

function registration_validation( $username, $password, $confirm_email, $email, $website, $bio ) {
global $reg_errors;
$reg_errors = new WP_Error;

if ( empty( $username ) || empty( $password ) || empty( $email ) ) {
$reg_errors->add('field', 'Required form field is missing');
}

if ( 4 > strlen( $username ) ) {
$reg_errors->add( 'username_length', 'Username too short. At least 4 characters is required' );
}
if ( username_exists( $username ) ){
$reg_errors->add('user_name', 'Sorry, that username already exists!');
}
if ( ! validate_username( $username ) ) {
$reg_errors->add( 'username_invalid', 'Sorry, the username you entered is not valid' );
}
if ( 5 > strlen( $password ) ) {
$reg_errors->add( 'password', 'Password length must be greater than 5' );
}
if ( !is_email( $email ) ) {
$reg_errors->add( 'email_invalid', 'Email is not valid' );
}
if ( $_POST['email'] !== $_POST['confirm_email'] ) {
$reg_errors->add( 'email_not_matched', "<strong>ERROR</strong>: E-mail is not match" );
}
if ( ! empty( $website ) ) {
if ( ! filter_var( $website, FILTER_VALIDATE_URL ) ) {
$reg_errors->add( 'website', 'Website is not a valid URL' );
}
}
if ( is_wp_error( $reg_errors ) ) {

foreach ( $reg_errors->get_error_messages() as $error ) {

echo '<div>';
echo '<strong>ERROR</strong>:';
echo $error . '<br/>';
echo '</div>';

}

}
}

function complete_registration() {
global $reg_errors, $username, $password, $email, $website, $bio;
if ( 1 > count( $reg_errors->get_error_messages() ) ) {
$userdata = array(
'user_login' => $username,
'user_email' => $email,
'user_pass' => $password,
'user_url' => $website,
'description' => $bio
);
$user = wp_insert_user( $userdata );
echo 'Registration complete. Goto <a href="' . get_site_url() . '/wp-login.php">login page</a>.';
}
}

function custom_registration_function() {
if ( isset($_POST['submit'] ) ) {
registration_validation(
$_POST['username'],
$_POST['password'],
$_POST['email'],
$_POST['confirm_email'],
$_POST['website'],
$_POST['description']
);

// sanitize user form input
global $username, $password, $email, $website, $bio;
$username = sanitize_user( $_POST['username'] );
$password = esc_attr( $_POST['password'] );
$email = sanitize_email( $_POST['email'] );
$website = esc_url( $_POST['website'] );
$bio = esc_textarea( $_POST['description'] );

// call @function complete_registration to create the user
// only when no WP_error is found
complete_registration(
$username,
$password,
$email,
$website,
$bio
);
}

registration_form(
$username,
$password,
$email,
$website,
$bio
);
}

// Register a new shortcode: [cr_custom_registration]
add_shortcode( 'cr_custom_registration', 'custom_registration_shortcode' );

// The callback function that will replace [book]
function custom_registration_shortcode() {
ob_start();
custom_registration_function();
return ob_get_clean();
}

?>




Widgets won't save


I have almost no experience in creating widget area's and I am stuck at saving the widgets in the widget area 'red menu'. I have searched a dozen posts but I can't seem to get it working.


I am currently working in Wordpress 4.0.1 and this is the code I have:


Widgets.php:



class Red_Menu_Widget extends WP_Widget {
function redmenu_widget( $args, $instance ) {
$widget_ops = array(
'classname' => 'redmenu_widget',
'description' => __('Description in here')
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'redmenu_widget' );

$this->WP_Widget( 'redmenu_widget', __('Rood menu rechts', 'redmenu_widget'), $widget_ops, $control_ops );
);
//parent::WP_Widget(false, 'Rood menu (rechts)', $widget_ops );
}
function widget() {
$title = 'Rood menu (rechts)';

echo $before_widget;
echo $title;
echo '<p>This is my widget output</p>';
echo $after_widget;
}
}
add_action( 'widgets_init', 'prefix_register_widgets' );
function prefix_register_widgets() {
register_widget( 'redmenu_widget' );
}


Functions.php:



function pfp_widgets_init() {

register_sidebar( array (
'name' => __( 'Rood menu (rechts)', 'powerforpaws' ),
'id' => 'redmenu_widget',
'description' => __( 'Descriptionhere', 'powerforpaws' ),
'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
'after_widget' => "</li>",
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
} // end pfp_widgets_init

add_action( 'widgets_init', 'pfp_widgets_init' );

// Pre-set Widgets
$preset_widgets = array (
'redmenu_widget' => array( 'search', 'pages', 'categories', 'archives' )
);

if ( !isset( $_GET['activated'] ) ) {
update_option( 'sidebars_widgets', $preset_widgets );
}


Sidebar.php:



<aside id="widgets">
<?php if ( !function_exists('redmenu_widget') || !dynamic_sidebar('redmenu_widget') ) : ?>
<div id="primary" class="widget-area">
<ul class="xoxo">
<?php dynamic_sidebar('redmenu_widget'); ?>
</ul>
</div><!-- #primary .widget-area -->
<?php endif; ?>
</aside>


What am I doing wrong?


Thanks in advance!





Display avatar of user profile when logged in


I am trying to display user's avatar/profile image when logged in, but when I use this:



<?php
global $current_user;
get_currentuserinfo();
echo get_avatar( $current_user->ID, 64 );


?>


it displays the default_avatar_male.jpg, but I don't want anything to be displayed unless logged in. Thanks for all help.





Altering next_posts_link(); and previous_posts_link();


We use following two functions to display pagination.



<?php next_posts_link( 'Older Entries »'); ?>

<?php previous_posts_link( 'Newer Entries »'); ?>


These above two function will output something like this



<a href="page link goes here">Older Entries »</a>
<a href="page link goes here">Newer Entries »</a>


Is there any way to add extra attribute with anchor link??


for e.g



<a href="page link goes here" class="name of the class" data-hover="Previous">Older</a>




add current-menu-item to multiple custom post types


I have two custom post types, and I have trouble adding the current-menu-item to both. In my functions.php I have following code:



function additional_active_item_classes($classes = array(), $menu_item = false){
global $wp_query;
if(in_array('current-menu-item', $menu_item->classes)){
$classes[] = 'current-menu-item';
}
if ( $menu_item->post_name == 'portfolio' && is_post_type_archive('portfolio') ) {
$classes[] = 'current-menu-item';
}
if ( $menu_item->post_name == 'portfolio' && is_singular('portfolio') ) {
$classes[] = 'current-menu-item';
}
if ( $menu_item->post_name == 'freebies' && is_post_type_archive('freebies') ) {
$classes[] = 'current-menu-item';
}
if ( $menu_item->post_name == 'freebies' && is_singular('freebies') ) {
$classes[] = 'current-menu-item';
}
return $classes;
}
add_filter( 'nav_menu_css_class', 'additional_active_item_classes', 10, 4 );


I know this is maybe not the most optimized code but most important to get it work. You can see the site here:


http://codingowl.net/


On portfolio it's working but not on the freebies. Why? Thanks so much in advance





Woocommerce Product Variation Attribute Values [on hold]


In Woocommerce 2.2.10 I have a product variation:



$variation = new WC_Product_Variation($variation_id);


and I want to get the attributes:



$attributes = $variation->get_variation_attributes();


returns



$attributes = array (
'attribute_color' => "mint-green",
'attribute_size' => "small"
)


But on the front-end in Woocommerce admin the attribute text as displayed in the color and size options is "Mint Green" and "Small", as opposed to the option values ("mint-green", "small"), which are returned by get_variation_attributes():


i.e.



<select name="attribute_color[0]">
<option value="">Any Color...</option>
<option selected="selected" value="mint-green">mint green</option>
</select>
<select name="attribute_size[0]">
<option value="">Any Size...</option>
<option selected="selected" value="small">Small</option>
</select>


Can I get product attributes in their original formatting?