Showing posts with label wordpress. Show all posts
Showing posts with label wordpress. Show all posts
Wordpress Advance Technique

Wordpress Advance Technique

Wordpress Advance Custom Field 

Wordpress ACF


Wordpress Advance Custom Field 

Dynamic dropdown for select ACF

add_filter('acf/load_field/name=field_name', 'loadAdminCombo');

function loadAdminCombo( $field ){

$result = getList();
if (!empty($result)){
$lang = getCurrentLanguage();
foreach ($result as $key => $obj) {
$key = "prefecture_name_$lang";
$val = $obj->$key;
$field['choices'][$obj->prefecture_id] = $val;
}
}
  return $field;
}

Show from the database

function getList( ) {
global $wpdb;
$sql = "SELECT * FROM wp_our_data WHERE status = '1' ";
$results = $wpdb->get_results($sql);

return $results;
}


SQL for WP pagination 

function getPrefectureList($area=null, $limit=SCREEN_PREFECTURE_LIMIT, $page = 1 ) {
global $wpdb;
$param = "";
if($area){
$param = " AND area_id='".$area."'";
}
$sql = "SELECT * FROM wp_prefectures WHERE status = '1' ".$param;

if ($limit) {
$sql .= " LIMIT ".$limit;
$offset = ($page-1)*$limit;
$sql .= " OFFSET ".$offset;
}
$results = $wpdb->get_results($sql);

return $results;
}

Ajax in Wordpress

add_action("wp_ajax_{do_ajax}", "run_ajax");
add_action("wp_ajax_nopriv_{do_ajax}", "run_ajax");

function run_ajax(){

echo 'ajax-ok'; die;
//admin_url('admin-ajax.php?action={do_ajax}')
}

here 
> do_ajax is common action to be performed
> we can add multiple ajax endpoint change this variable
> Endpoint created is  : admin_url('admin-ajax.php?action={do_ajax}')


Wp retrieve GET param


$prama = get_query_var('param_name');

Wordpress Load script in Admin


function my_enqueue($hook) {

    wp_enqueue_script('admin_js', get_template_directory_uri() . '/js/admin.common.js');
}

add_action('admin_enqueue_scripts', 'my_enqueue');
Wordpress notes

Wordpress notes


3 technique Creating Custom page


- conditional approach if(is_page())
- template file with page- followed by slug or page id [page-*]
- custom template directly by  different page name . Then select that template from admin panel

function


- get_header(); //render header content
- get_footer();  // render footer content


The post function

- have_posts(); // return true/false
- the_post(); // return post
         - get_the_author();
         - get_the_date({php date string});

- the_content('continue reading..');  // for single post
- the_excerpt(); // use in view file result for all post
this will add input excerpt in admin post edit...
- get_the_excerpt(); // we need to add manual link to detail
- the_permalink();

- the_time('php_date_string'); // get posted date

-the_title();  // get title for post
- the_author();  // get author for post
- get_author_post_url({author_id});
- get_the_author_meta('ID');  // retirn author_id for given posi_id {ID}


To check for archive...

// url type
- is_category();  // if post has category boolean
- is_tag();
- is_day();
- is_month();
- is_year();

Category function

- get_the_category();  // return array of category for the post
- get_category_link({category_id}); // return category link

for loop to get single one..
$categories = get_the_category();
foreach($categories as $category) {
      $catname = $category-> cat_name ;
      $catId = $category->term_id;

}


Feature Image
function func_to_call_theme_support(){
// add feature image support
add_theme_support();

//image size
//add_image_size('thumb_name', width,height , true);
add_image_size('small_thumb', 120, 150 , true);
//crop image with 4th param
add_image_size('small_thumb', 120, 150 , array('left','top')); // crop form lrft top
add_image_size('banner_thumb', 120, 150 , array('left','top')); // crop form lrft top
// or we can crop from admin panel
}
add_action('after_setup_theme','func_to_call_theme_support');

//to use image in view file

- the-post_thumbnail('small_thumb');


Pages

- homepage template. page.php > front-page.php