21 Oct 2019

how to add custom field in custom post type wordpress

This is the hardest of the lot. It’s not really hard though, just alot of HTML to make the meta boxes. You can put any HTML you like in meta boxes, I just like to keep mine looking like standard WP fields:


<?php
/**
 * Add cafe custom fields
 */
function add_cafe_meta_boxes() {
 add_meta_box("cafe_contact_meta", "Contact Details", "add_contact_details_cafe_meta_box", "cafes", "normal", "low");
}
function add_contact_details_cafe_meta_box()
{
 global $post;
 $custom = get_post_custom( $post->ID );
 
 ?>
 <style>.width99 {width:99%;}</style>
 <p>
  <label>Address:</label><br />
  <textarea rows="5" name="address" class="width99"><?= @$custom["address"][0] ?></textarea>
 </p>
 <p>
  <label>Website:</label><br />
  <input type="text" name="website" value="<?= @$custom["website"][0] ?>" class="width99" />
 </p>
 <p>
  <label>Phone:</label><br />
  <input type="text" name="phone" value="<?= @$custom["phone"][0] ?>" class="width99" />
 </p>
 <?php
}
/**
 * Save custom field data when creating/updating posts
 */
function save_cafe_custom_fields(){
  global $post;
 
  if ( $post )
  {
    update_post_meta($post->ID, "address", @$_POST["address"]);
    update_post_meta($post->ID, "website", @$_POST["website"]);
    update_post_meta($post->ID, "phone", @$_POST["phone"]);
  }
}
add_action( 'admin_init', 'add_cafe_meta_boxes' );
add_action( 'save_post', 'save_cafe_custom_fields' );


Custom Templates

You may want your new post type to look different from standard posts. This can be done using custom templates. The WordPress codex cover custom post type templates nicely but I’ll quickly go over what I did for completeness. I’m using twentytwelve theme in my demo but this should apply for most themes.
  1. Duplicate single.php into single-<post type>.php
  2. Replace
    <?php get_template_part( 'content', get_post_format() ); ?>
    with
    <?php get_template_part( 'content', '<post type>' ); ?>
  3. Duplicate content.php into content-<post type>.php and modify however you like




Reference Links:


26 Sept 2019

how to rename the multiple filenames in windows | Using Command Prompt



You could also try using PowerShell, a powerful Windows command line tool. You'd run this command:


Open the CMD and go to the file location 

then 

Enter "PowerShell"


Full Command:

get-childitem *.mp3 | foreach { rename-item $_ $_.Name.Replace("Radiohead -", "") }
Analyzing it:
get-childitem *.mp3
This lists all files whose names end with .mp3.  They are then piped to the next command with the | operator.
foreach { rename-item $_ $_.Name.Replace("Radiohead -", "") }
This replaces all instances of Radiohead - with nothing, denoted by "", effectively wiping the word from all the files in the directory.

You could also modify get-childitem *.mp3 to get-childitem – that would rename all the files in the directory, not just files whose names end with .mp3.


24 Jul 2019

How To Use Ajax With jQuery In WordPress

jQuery Ajax in WordPress

Implementing WordPress Ajax is so simple actually. We will see a practical example of it. Let’s say we have tables wp_countries and wp_states. Our task is – On the selection of country display the respective states in the drop-down. In the below tables we have the one-to-many relationship between country and states.

Table Country



Table States

To build our country drop-down let’s first fetch the countries from the database and build the drop-down from it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
global $wpdb;
$aCountries = $wpdb->get_results( "SELECT id, country FROM ".$wpdb->prefix."countries" );
<form method="post">
  <select class="countries">
    <option value="">--SELECT COUNTRY--</option>
    <?php foreach ($aCountries as $country) { ?>
      <option value="<?php echo $country->id; ?>"><?php echo $country->country; ?></option>
    <?php } ?>
  </select>
  <div class="load-state"></div>
</form>
?>
We have added the class ‘countries’ to select tag which would use in jQuery code. Also, We have added an empty div with class ‘load-states’ which will use to append state dropdown after Ajax response.
Now let’s write a jQuery code which acts as a bottleneck for our task.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<script type="text/javascript">
  var ajaxurl = '<?php echo admin_url("admin-ajax.php"); ?>';
  jQuery(function($) {
    $('body').on('change', '.countries', function() {
      var countryid = $(this).val();
      if(countryid != '') {
        var data = {
          action: 'get_states_by_ajax',
          country: countryid,
          'security': '<?php echo wp_create_nonce("load_states"); ?>'
        }
        $.post(ajaxurl, data, function(response) {
          $('.load-state').html(response);
        });
      }
    });
  });
</script>
At first we declared the variable ajaxurl. This is because in WordPress each Ajax request should send to admin-ajax.php file. So we store file path in our variable. Then we declared variable with the name data. It contains parameter action. The action parameter is user-defined name which should be task-oriented. As we are fetching states so I keep the name get_states_by_ajax. Rest of the parameters are the details should send to Ajax request. As we have to pass country id we keep the parameter name as country and value for this parameter is countryid.
Now it’s time to write actual Ajax code in a WordPress way. Write the below Ajax code in your active theme’s functions.php file.
1
2
add_action('wp_ajax_get_states_by_ajax', 'get_states_by_ajax_callback');
add_action('wp_ajax_nopriv_get_states_by_ajax', 'get_states_by_ajax_callback');
wp_ajax is a fixed prefix while we are writing Ajax code in WordPress. Then append the action parameter value to it. In our case it is get_states_by_ajax. So, first parameter becomes wp_ajax_get_states_by_ajax. Second parameter is a callback function in which we have to write our actual code. wp_ajax_nopriv should use when we are doing operations for front end.
All is set. Now we can write the code which will return the states in the HTML drop-down format.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function get_states_by_ajax_callback() {
  check_ajax_referer('load_states', 'security');
  $country = $_POST['country'];
  global $wpdb;
  $aStates = $wpdb->get_results( $wpdb->prepare( "SELECT id, state_name FROM ".$wpdb->prefix."states WHERE cid = %d", $country ) );
  if ( $aStates ) {
    ?>
    <select>
      <?php
      foreach ($aStates as $state) {
        ?>
        <option value="<?php echo $state->id; ?>"><?php echo $state->state_name; ?></option>
        <?php
      }
      ?>
    </select>
    <?php
  }
  wp_die();
}
Above response will send to our jQuery code. And using jQuery we append the states drop-down in the div container with class ‘load-state’.


Source Link: Click Here