Google help on software/programming And Windows Tricks: April 2015

helponsoftware

Tuesday 28 April 2015

calculate the maximum length of palindrome by given any array of names or string

<?php
   function PalindromeLengthPuzzle($input1)
    {
$polin_array =array();
foreach ($input1 as $key => $value) {
  $res[]= substr($value,0,1);
}
print_r($res);
for($c=0;$c<=1000;$c++):
shuffle($res);

$result= implode('',$res);

if($result==strrev($result)){
 $polin_array[]=$result;   
}

endfor;
$polin_array=array_unique($polin_array);
print_r($polin_array);

foreach ($polin_array as $key => $value) {
    $val =strlen($value);
}
echo $val;
}

echo PalindromeLengthPuzzle(array('Bharti', 'Bharat','Brijesh','Bhavna','Anjit','Chand','Aushil'));
?>

Monday 27 April 2015

Wordpress Login by Any page

<?php
include('../impact windows/wp-load.php');
?>
<form method="post" action="http://localhost/anjit/impact%20windows/wp-login.php">
<input type="text" name="log">
<input type="password" name="pwd">
<input type="submit" name="sumit">
<input type="hidden" name="redirect_to" value="http://localhost/anjit/impact%20windows/wp-admin/edit.php?post_type=service" />

</form>

Image upload from frontend in wordpress

function insert_attachment() {
$uploaddir = wp_upload_dir();
$file = $_FILES['poster'];
$uploadfile = $uploaddir['path'] . '/' . basename( $file['name'] );

move_uploaded_file( $file['tmp_name'] , $uploadfile );
$filename = basename( $uploadfile );

$wp_filetype = wp_check_filetype(basename($filename), null );

$attachment = array(
    'post_mime_type' => $wp_filetype['type'],
    'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
    'post_content' => '',
    'post_status' => 'inherit',
    'menu_order' => $_i + 1000
);
$attach_id = wp_insert_attachment( $attachment, $uploadfile );
$fullsizepath = get_attached_file( $attach_id );
$metadata = wp_generate_attachment_metadata( $attach_id, $fullsizepath );
wp_update_attachment_metadata( $attach_id,  $metadata );

if ( is_wp_error( $metadata ) )
echo 'Images not upoaded Correctly Please try Again.';
if ( empty( $metadata ) )
echo 'Unknown failure reason.';

return $attach_id;
}

Wordpress Media uploader in frontend

<label for="image_url">Photo</label>
<input type="hidden" name="image_url" id="image_url" class="regular-text">
<input type="hidden" name="attachement_id" id="image_id" class="regular-text">
<div id="img_btn"><input type="button" name="upload-btn" id="upload-btn" class="button-secondary" value="Upload Image"></div>
<?php wp_nonce_field( 'my_image_upload', 'my_image_upload_nonce' ); ?>

<script type="text/javascript">
jQuery(document).ready(function($){
$('#upload-btn').click(function(e) {
e.preventDefault();
var image = wp.media({
title: 'Upload Image',
// mutiple: true if you want to upload multiple files at once
multiple: false
}).open()
.on('select', function(e){
// This will return the selected image from the Media Uploader, the result is an object
var uploaded_image = image.state().get('selection').first();
// We convert uploaded_image to a JSON object to make accessing it easier
// Output to the console uploaded_image
//console.log(uploaded_image);
var image_url = uploaded_image.toJSON().url;
var id = uploaded_image.toJSON().id;
// Let's assign the url value to the input field
$('#image_url').val(image_url);
$('#image_id').val(id);
});
});
});

</script>


USE below FUNCTION IN " functions.php"
wp_enqueue_media();

Wordpress login by user Role

add_action('login_form', 'wdm_login_form_role');
function wdm_login_form_role()
{
 ?>
<p><label for="user_role">Role<br>
<select name="role" style="flaot:left;width:100%;margin-bottom:10px;padding: 10px;">
<option value="publishers">Publishers</option>
<option value="advertisers">Advertisers</option>
</select></label>
<p>
 <?php
  }
add_filter('wp_authenticate_user','wdm_validate_login_role',10,2);
function wdm_validate_login_role($user, $password) {

   $ruser= get_user_by('login',$_POST['log']);

  if($ruser){
       if($ruser->roles[0]==$_POST['role']){
          return $user;
                   }
      else{
     return null;
      }
 }
}