flush_rules(); akbar_settings_json_stuff(); } //add_action('after_switch_theme', 'akbar_setup_theme_options'); //add_action('admin_init', 'akbar_setup_theme_options'); function akbar_settings_json_stuff() { $access_type = get_filesystem_method(); // if ($access_type === 'direct') { /* you can safely run request_filesystem_credentials() without any issues and don't need to worry about passing in a URL */ $creds = request_filesystem_credentials(home_url() . '/wp-admin/', '', false, false, array()); /* initialize the API */ if (!WP_Filesystem($creds)) { /* any problems and we exit */ return false; } global $wp_filesystem; if (false === ($creds = request_filesystem_credentials(esc_url_raw(home_url()), "", false, false, ['save']))) { return true; // Stop the normal page form from displaying, credential request form will be shown. } WP_Filesystem($creds); $string = akbar_theme_options_generation(); $wp_filesystem->put_contents(get_template_directory() . "/out/assets/settings.js", $string); return $wp_filesystem; // } else { // /* don't have direct write access. Prompt user with our notice */ // add_action('admin_notices', 'akbar_notice_function'); // } } function akbar_theme_options_generation_array() { global $AKBAR_API; $custom_logo_id = get_theme_mod('custom_logo'); $logo = wp_get_attachment_image_src($custom_logo_id, 'full'); // Get option settings from database if (!$logo[0]) { $logo[0] = ''; } $options = ['akbar_header_logo_image' => $logo[0]]; $home_url = home_url(); $footerm = $AKBAR_API->wp_api_v2_locations_get_menu_data(['id' => 'footerm']); if (!$footerm) { $footerm = new stdClass(); $footerm->items = []; } $headerm = $AKBAR_API->wp_api_v2_locations_get_menu_data(['id' => 'headerm']); if (!$headerm) { $headerm = new stdClass(); $headerm->items = []; } $mainmenu = $AKBAR_API->wp_api_v2_locations_get_menu_data(['id' => 'mainmenu']); if (!$mainmenu) { $mainmenu = new stdClass(); $mainmenu->items = []; } if ($mainmenu && $mainmenu->items) foreach ($mainmenu->items as $key => $m) { if ($m) $mainmenu->items[$key]->url = str_replace($home_url, "", $m->url); } $arr = [ 'home_url' => home_url(), 'rest_url' => get_rest_url(), 'blog_title' => get_bloginfo('name'), 'theme_uri' => get_template_directory_uri(), 'theme_options' => $options, 'menu' => [ 'headerm' => $headerm, 'footerm' => $footerm, 'mainmenu' => $mainmenu ] ]; return $arr; } function akbar_theme_options_generation() { $arr = akbar_theme_options_generation_array(); $string = "window.globalTS=" . json_encode($arr); return $string; } add_action('admin_notices', 'akbar_notice_function'); function akbar_notice_function() { $message = 'After changing menu, theme customization and changing front page please click on =>'; printf('
%sCreate
', esc_html($message)); $message = 'then please go to your home page and click on CTRL+SHIFT+R '; printf('
%shome page
', esc_html($message)); } function akbar_customizer_live_preview() { wp_enqueue_script( 'akbar-themecustomizer', //Give the script an ID AKBAR_INCLUDES_URL . '/dist/js/theme-customizer.js',//Point to file array('jquery', 'customize-preview'), //Define dependencies '', //Define a version (optional) true //Put script in footer? ); } add_action('customize_preview_init', 'akbar_customizer_live_preview'); add_action('admin_enqueue_scripts', 'akbar_ajax_enqueue'); function akbar_ajax_enqueue($hook) { // if ('index.php' != $hook) { // // Only applies to dashboard panel // return; // } wp_enqueue_script('akbar-ajax-script', AKBAR_INCLUDES_URL . '/dist/js/akbar-ajax.js', array('jquery')); // in JavaScript, object properties are accessed as ajax_object.ajax_url, ajax_object.we_value wp_localize_script('akbar-ajax-script', 'ajax_object', array('ajax_url' => admin_url('admin-ajax.php'), 'akbar_we_value' => 1234)); } // Same handler function... add_action('wp_ajax_akbar_ajax_action', 'akbar_ajax_action'); function akbar_ajax_action() { akbar_setup_theme_options(); echo esc_html('done'); wp_die(); }