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');