How to create admin panel in wordpress - php

function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'woocommerce_payu_in' ),
'type' => 'checkbox',
'label' => __( 'Enable PayUMoney', 'woocommerce_payu_in' ),
'default' => 'yes'
)
);
}
I am getting this code from pay-u plugin. Using this code I want to create my own admin panel, but I don't understand what this code means.

This is the tutorial page of wordpress where you can find a solution to add a admin page.
https://codex.wordpress.org/Administration_Menus
Wordpress Information:
Here is a very simple example of the three steps just described. This
plugin will add a sub-level menu item under the Settings top-level
menu, and when selected, that menu item will cause a very basic screen
to display. Note: this code should be added to a main plugin PHP file
or a separate PHP include file.
This is the wordpress example code:
<?php
/** Step 2 (from text above). */
add_action( 'admin_menu', 'my_plugin_menu' );
/** Step 1. */
function my_plugin_menu() {
add_options_page( 'My Plugin Options', 'My Plugin', 'manage_options', 'my-unique-identifier', 'my_plugin_options' );
}
/** Step 3. */
function my_plugin_options() {
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
echo '<div class="wrap">';
echo '<p>Here is where the form would go if I actually had options.</p>';
echo '</div>';
}
?>

Related

WooCommerce PHP: Display data from custom meta created in functions.php

I recently created a custom field in functions.php to display a custom meta field in the Inventory tab for each product called barcode. What I now want to do is display the data onto my single product template using a PHP call. Here's first of all, the function to create the barcode data (which works):
function add_barcode(){
woocommerce_wp_text_input(
array(
'id' => '_barcode',
'label' => __( 'Barcode', 'your-plugin' ),
'placeholder' => 'Scan Barcode',
'desc_tip' => 'true',
'description' => __( "Scan the product's barcode.", "your-plugin" )
)
);
}
add_action('woocommerce_product_options_inventory_product_data','add_barcode');
function add_barcode_save( $product ){
if( isset( $_POST['_barcode'] ) ) {
$product->update_meta_data( '_barcode', sanitize_text_field( $_POST['_barcode'] ) );
} else {
$product->delete_meta_data( '_barcode' );
}
}
add_action( 'woocommerce_admin_process_product_object', 'add_barcode_save' );
Here's the PHP I'm trying to generate that data onto the page:
$product = wc_get_product();
echo '<strong>Barcode: </strong>' .$product->get_attribute( 'barcode' );
All I get here is a blank area after the HTML portion. So what function will allow me to display the barcode text?

How to add a custom checkbox to select all checkboxes on woocommerce checkout page to the woocommerce_review_order_before_submit?

I need to add a checkbox with the option to select all the checkboxes below it on the checkout page in woocommerce. How could I do this using php or javascript ?? Or if there is another better option, I will be happy.
So far, I have used the "code snippets" plugin to add checkboxes. But there's no way to add the feature I need ... at least I think so. :)
Thank you in advance for everything ... I have used this code on the plugin...
add_action( 'woocommerce_review_order_before_submit', 'bt_add_checkout_checkbox', 10 );
/**
* Add WooCommerce Checkbox checkout
*/
function bt_add_checkout_checkbox() {
woocommerce_form_field( 'checkout-checkbox', array( // CSS ID
'type' => 'checkbox',
'class' => array('form-row mycheckbox'), // CSS Class
'label_class' => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'),
'input_class' => array('woocommerce-form__input woocommerce-form__input-checkbox input-checkbox'),
'required' => true, // Mandatory or Optional
'label' => 'Ok I agree with Checkout Checkbox Page', // Label and Link
));
}
add_action( 'woocommerce_checkout_process', 'bt_add_checkout_checkbox_warning' );
/**
* Alert if checkbox not checked
*/
function bt_add_checkout_checkbox_warning() {
if ( ! (int) isset( $_POST['checkout-checkbox'] ) ) {
wc_add_notice( __( 'Please acknowledge the Checkbox' ), 'error' );
}
}
Can I use it also for select all checkbox??
Just add a js file with your function bt_add_checkout_checkbox_warning(), or like this in functions.php file with child theme.
function my_hook() {
if ( is_page(your page id here) ) { // use is_page or is_post to identify page
wp_enqueue_script('custom', get_stylesheet_directory_uri().'/js/custom.js');
}
}
add_action( 'wp_enqueue_scripts', 'my_hook' );
create another checkbox id #checkAll, for example, so you can toggle like this in your custom.js file
$("#checkAll").click(function () {
$('.input-checkbox').prop('checked', this.checked);
});
jsfiddle

How to overwrite all login forms in WordPress

I want to override the login form as follows:
function my_login_form() {
$login_type = 'login';
change_login_form( $login_type );
}
add_action( 'login_form', 'my_login_form' );
For that, I use ob_start(). However, the default form is output.
function change_login_form( $login_type ){
// Remove default form ( Not going well )
ob_start();
ob_clean();
wp_head();
// Add own form
$content = html_form( $login_type );
ob_end_clean();
echo $content;
}
What can I do to override the default form?
You can create a custom login page that overrides the default WordPress one. There is a section on this page called "Make a Custom Login Page" if you scroll down a bit. WP Codex : Customizing the Login Form
You can customize the inputs by passing parameters to the wp_login_form() function in the page-login.php of your theme folder.
<?php
if ( ! is_user_logged_in() ) { // Display WordPress login form:
$args = array(
'redirect' => admin_url(),
'form_id' => 'loginform-custom',
'label_username' => __( 'Username custom text' ),
'label_password' => __( 'Password custom text' ),
'label_remember' => __( 'Remember Me custom text' ),
'label_log_in' => __( 'Log In custom text' ),
'remember' => true
);
wp_login_form( $args );
} else { // If logged in:
wp_loginout( home_url() ); // Display "Log Out" link.
echo " | ";
wp_register('', ''); // Display "Site Admin" link.
}
?>

Custom menus not showing in Menu Settings in WordPress

I'm a WordPress beginner working on a local server I set up with MAMP. I created the style, index, footer, header and functions docs, and PHP assembled everything with no problems. The website published all my files as expected. Then I tried to add custom menus to the appearance panel / menus in the admin dashboard, but they wouldn't show up.
Here's a snapshot of my folder hierarchy:
And here's a screenshot of the admin page:
And this is all the code in my functions.php file so far:
<?php
function macsc_script_enqueue() {
wp_enqueue_style('customstyle', get_template_directory_uri() . '/css/macsc.css', array(), '1.0.0', 'all');
wp_enqueue_script('customjs', get_template_directory_uri() . '/js/macsc.js', array(), '1.0.0', true);
}
add_action('wp_enqueue_scripts', 'macsc_script_enqueue');
function register_my_menus() {
register_nav_menus(
array(
'header-menu' => __( 'Header Menu' ),
'extra-menu' => __( 'Extra Menu' )
)
);
}
add_action( 'init', 'register_my_menus' );
I've read several stack overflow threads and consulted the WordPress Codex support documentation. The register_my_menus function was a direct copy and paste from the WordPress doc. As far as I can tell, I'm doing everything correctly (obviously not, of course).
One thing that seems odd to me is that there were already tabs for "widgets," "menus" and "header" in the Appearance panel. Considering this is a custom theme that started with an empty folder, I'm not sure why those are there at all.
Any help would be greatly appreciated.
Typically menus are registered on the after_setup_theme hook. I'd try using that instead of init.
Also you don't need to register custom widgets or widget area support for the Widgets menu to show up, there's a lot that WordPress introduces in the core, regardless of your theme or active plugins.
And on second glance, it appears the TwentySeventeen theme is active, considering the Top Menu and Social Links Menu are the default menus in the TwentySeventeen (default) theme.
From TwentySeventeen/functions.php:
register_nav_menus( array(
'top' => __( 'Top Menu', 'twentyseventeen' ),
'social' => __( 'Social Links Menu', 'twentyseventeen' ),
) );
Go to Appearance > Themes and make sure your custom theme is activated.
I always register the menu in this way:
if ( !function_exists( 'theme_setup' ) ) {
function theme_setup() {
/*
* Some settings functions
*/
register_nav_menus(
array(
'header-menu' => __( 'Header Menu', 'domain' ),
'extra-menu' => __( 'Extra Menu', 'domain' )
)
);
}
}
add_action( 'after_setup_theme', 'theme_setup' );

Wordpress - Buddypress plugin

I want to hide sub-nav in profile settings
I hide sub-nav comment "wp-content\plugins\buddypress\bp-settings\bp-settings-loader.php"
// Add General Settings nav item
$sub_nav[] = array(
'name' => __( 'General', 'buddypress' ),
'slug' => 'general',
'parent_url' => $settings_link,
'parent_slug' => $this->slug,
'screen_function' => 'bp_settings_screen_general',
'position' => 10,
'user_has_access' => bp_core_can_edit_settings()
);
What sub-nav item are you referring to? If you want to remove the Settings menu option entirely you can do this in a plugin or functions.php
function my_admin_bar_mod(){
global $wp_admin_bar;
$wp_admin_bar->remove_menu( 'my-account-settings' );
}
add_action('wp_before_admin_bar_render','my_admin_bar_mod');
To remove just the Profile option under Settings use this instead:
$wp_admin_bar->remove_menu( 'my-account-settings-profile' );
UPDATE:
The following code will remove the General tab; I believe that is what you want. Correct? This code does that but I am seeing a problem. It might be a rewrite problem on my dev site where the Settings tab causes a 4040 error. Can you try this on your site and let me know?
function mcs_bp_remove_nav() {
global $bp;
bp_core_remove_subnav_item( $bp->settings->slug, 'general' );
}
add_action( 'bp_setup_nav', 'mcs_bp_remove_nav', 99);
Finally:
This code is needed in addition to the above. It changes Settings to point to the Email tab. It was defaulting to General and since that was removed we see a 404. This hook must fire earlier than the code that removes 'general'.
function mcs_bp_change_settings() {
global $bp;
// point setting to Email page (aka 'notifications')
$args = array( 'parent_slug' => 'settings',
'screen_function' => 'bp_core_screen_notification_settings',
'subnav_slug' => 'notifications'
);
bp_core_new_nav_default( $args );
}
add_action( 'bp_setup_nav','mcs_bp_change_settingst', 5);

Categories