Ordering input fields in WordPress General Settings page - php

I have created a plugin that adds a input box, 'Logo URL' on the Settings > General page in WordPress. This input can be called and works correctly. I have created another plugin that pulls the 'Logo URL' and applies the path to pull an image for the Login screen. Everything appears peachy.
The only issue I am having is that I would like to move the 'Logo URL' on the Settings > General page to up under 'Site Address (URL)'. I am at a loss on how to do this. I have scoured the web and been unable to find a helpful answer.
I am currently removing the original General page and adding a New General page but am unsure how to parse the correct options-general.php.
How to move the Logo_URL higher on the General Page?
/**
* This is the code to create the Settings box on the Settings > General
*/
$new_general_setting = new new_general_setting();
class new_general_setting {
function new_general_setting( ) {
add_filter( 'admin_init' , array( &$this , 'register_fields' ) );
}
function register_fields() {
register_setting( 'general', 'URL_logo', 'esc_attr' );
add_settings_field('URL_logo', '<label for="URL_logo">'.__('Website logo (URL)' , 'URL_logo' ).'</label>' , array(&$this, 'fields_html') , 'general' );
}
function fields_html() {
$value = get_option( 'URL_logo', '' );
echo '<input type="text" id="URL_logo" name="URL_logo" value="' . $value . '" />';
}
}

No, there's no way of ordering that natively. WordPress first prints its stuff then ours. It has to be done with jQuery.
add_action( 'admin_footer-options-general.php', function()
{
?>
<script type="text/javascript">
jQuery(document).ready( function($)
{
var son = $("label[for='URL_logo']").parent().parent(); // Our setting field
var father = $("label[for='home']").parent().parent(); // WordPress setting field
son.insertAfter(father);
});
</script>
<?php
});
The recommended way is to enqueue the JS inside an action call for "admin_print_scripts-$hookname". Note the hook name use in admin_footer and admin_head too.
As your field only changes after the page loaded, we can notice the "jump". To smooth it, we can use:
add_action( 'admin_head-options-general.php', function()
{
echo '<style>#wpbody .wrap form{display:none}</style>';
});
And add this jQuery after replaceWith():
$('#wpbody .wrap form').fadeIn('slow');

Related

Dynamically download correct pdf from file custom field after contact form 7 submission based on page you're in

Hi,
i have a site that displays various projects as posts, each project has its own pdf file, uploaded via a file custom field for that post.
For the moment there's just a dynamic button as you can see in this example:
https://www.stefanomengoli.it/sm21/progetti/vivere-nelle-nuvole-progetto-di-bioarchitettura-per-un-loft-casa-sullalbero/
What i need is the user to be able to download or be redirected to the correct file after CF7 submission based on the project he/she is on.
I tried this code and it works with a specific url, but what i need is to put an acf url that dynamically shows the correct file as I said based on the project the visitor is on.
add_action( 'wp_footer', 'example_download' );
function example_download() {
?>
<script type="text/javascript">
document.addEventListener( 'wpcf7mailsent', function( event ) {
if ( '946' == event.detail.contactFormId ) {
window.open('https://www.example.com/wp-content/uploads/2021/05/my-document.php',
'_self');
}
}, false );
</script>
<?php
}
You just need to echo the custom field in the window.open
This should work. Just replace your_field with your ACF field name
add_action( 'wp_footer', 'example_download' );
function example_download() {
global $post;
?>
<script type="text/javascript">
document.addEventListener('wpcf7mailsent', function (event) {
if ('946' == event.detail.contactFormId) {
window.open('<?php the_field('your_field', $post->ID);?>',
'_self');
}
}, false);
</script>
<?php
}

Using wordpress action to modify div content

I'm trying to modify a div's content everytime when something is added to the woocommerce shopping cart. For this example the content is gonna be the current total cart value.
So first I created a simple plugin called "test-cart-value" which contains the following code:
<?php
function test_cart_value() {
echo "<div>" . WC()->cart->total . "</div>";
}
add_shortcode('test_cart_value_shortcode', 'test_cart_value');
This works fine, wherever I place the shortcode I get the current cart value after page load.
So, now I want this printed value to updated every time something is added to the cart, without reloading the page. The idea was to use the action hook woocommerce_cart_updated and call the function - so everytime something in the cart changes, the new cart value gets echoed:
function action_woocommerce_cart_updated() {
test_cart_value();
};
// add the action
add_action( 'woocommerce_cart_updated', 'action_woocommerce_cart_updated', 10, 0 );
The problem is, now I'm not able to dynamically add products to the shopping cart. Whenever I hit the "add to cart" button, the loading animation loads forever.
How to do this properly?
I was trying different approaches with Ajax and different Hooks, but so far nothing worked.
Any Ideas? Thanks in advance!
Edit:
So I tried this as my plugin code
function test_cart_value() {
echo "<div id='cart_test'>" . WC()->cart->total . "</div>";
}
add_shortcode('test_cart_value_shortcode', 'test_cart_value');
// define the actions for the two hooks created, first for logged in users and the next for logged out users
add_action("woocommerce_cart_updated", "cart_update");
// define the function to be fired for logged in users
function cart_update() {
$cart = WC()->cart->total;
$result['type'] = "success";
$result['new_cart'] = $cart;
$result = json_encode($result);
//if I uncomment the "die" function, the page won't load
// die();
}
// Fires after WordPress has finished loading, but before any headers are sent.
add_action( 'init', 'script_enqueuer' );
function script_enqueuer() {
// Register the JS file with a unique handle, file location, and an array of dependencies
wp_register_script( "test_script", plugin_dir_url(__FILE__).'test_script.js', array('jquery') );
// localize the script to your domain name, so that you can reference the url to admin-ajax.php file easily
wp_localize_script( 'test_script', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));
// enqueue jQuery library and the script you registered above
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'test_script' );
}
And my test_script.js code:
jQuery(document).ready( function() {
jQuery(".ajax_add_to_cart").click( function(e) {
e.preventDefault();
jQuery.ajax({
type : "post",
dataType : "json",
url : myAjax.ajaxurl,
data : {action: "cart_update"},
success: function(response) {
if(response.type == "success") {
jQuery("#cart_test").html(response.new_cart);
}
else {
alert("Your like could not be added");
}
}
});
});
});
So I thought that the cart_update() function should fire when I press the "ajax_add_to_cart" Button, but I get an error 400.
Any ideas?
Thanks!
made it work with woocommerce_add_to_cart_fragments.

Hide element with specific css selector in WordPress

I'm using the WordPress plugin "restrict content pro".
I would like to hide an element with selector li#wp-admin-bar-my-account-messages for a specific subscription id with PHP within my functions.php file.
I guess the PHP validation is rcp_is_active() && rcp_get_subscription_id() == 2 but don't know how to go on from here.
Thanks in advance
Assuming that rcp_is_active() && rcp_get_subscription_id() == 2 will validate to your specific use case and you just want to hide the element which has selector li#wp-admin-bar-my-account-messages you can do the following in your function.php
if ( rcp_is_active() && rcp_get_subscription_id() == 2 )
{
// for frontend
add_action( 'wp_head', function() {
echo '<style type="text/css">li#wp-admin-bar-my-account-messages{display:none !important}</style>';
} );
// for backend
add_action( 'admin_head', function() {
echo '<style type="text/css">li#wp-admin-bar-my-account-messages{display:none !important}</style>';
} );
}
Hope it helps :)

How to enable additional page in WordPress custom plugin?

I have a custom WordPress plugin that is showing me a list of data from database. I am registering it's page via:
add_menu_page(
'Naročila',
'Vsa naročila',
'administrator',
'listaj-narocila',
array( &$this, 'listaj_narocila' )
);
And then of course I have function lista_narocila which is showing me my data.
So, currently my URL is:
http://domain.com/wp-admin/admin.php?page=listaj-narocila
And I show my data from database in a table. Now I have button DELETE and EDIT for each record, but I have a hard time figuring it out, how to register custom "url" or "custom page" inside WordPress that would allow me to have URL:
http://domain.com/wp-admin/admin.php?page=single-narocilo?id=X
I know I can try with add_menu_page but I don't want this page to be in admin menus. Just to be available as URL. Currently I get no access error.
You can create a sub menu page and pass null as its parent:
$parent_slug
Use NULL or set to 'options.php' if you want to create a page that doesn't appear in any menu.
A demo:
add_action('admin_menu', function()
{
# Main page
add_menu_page(
'Vsa',
'Vsa',
'add_users', // Capability, not role
'listaj-narocila',
function(){
printf(
'<h2>%s</h2>%s',
__( 'Main page' ),
admin_url( 'admin.php?page=single-norcilo&id='.rand(1,25) ),
__( 'Hidden sub page' )
);
},
'http://sstatic.net/stackexchange/img/favicon.ico'
);
# Child page
$hook = add_submenu_page(
null,
'Norcilo',
'Norcilo',
'add_users',
'single-norcilo',
function(){
printf(
'<h2>%s</h2>%s',
__( 'Hidden sub page' ),
admin_url( 'admin.php?page=listaj-narocila' ),
__( 'back' )
);
}
);
# Enqueue script in submenu page to fix the current menu indicator
add_action( "admin_footer-$hook", function()
{
echo <<<HTML
<script type="text/javascript">
jQuery(document).ready( function($) {
$('#toplevel_page_listaj-narocila')
.removeClass('wp-not-current-submenu')
.addClass('current');
});
</script>
HTML;
});
});
An alternative approach: https://wordpress.stackexchange.com/a/114818/12615

Using wordpress color picker in post options

I create post options and I want to implement wordpress color picker core inside it
I tried this code I got it from many tutorials and sources but unfortunately It's not working at all, It's like I never added the code.
HTML
<input name="mv_cr_section_color" type="text" id="mv_cr_section_color" value="#ffffff" data-default-color="#ffffff">
PHP
function Colorpicker(){
wp_enqueue_style( 'wp-color-picker');
wp_enqueue_script( 'wp-color-picker');
}
add_action('admin_enqueue_scripts', 'Colorpicker');
JQuery
jQuery(document).ready(function(){
jQuery('#mv_cr_section_color').wpColorPicker();
});
You don't say how you're creating the Theme Options page, but the following is a working example. It's almost the same code as your sample code, but the enqueue is done directly on the custom menu page callback and jQuery is being referenced as $ (note its declaration in ready(function($)):
<?php
/**
* Plugin Name: Testing the Color Picker
*/
add_action( 'admin_menu', 'b5f_demo_menu' );
function b5f_demo_menu()
{
add_menu_page(
'Test',
'Test',
'edit_pages',
'test-slug',
'b5f_callback_function'
);
}
function b5f_callback_function()
{
wp_enqueue_script('wp-color-picker');
wp_enqueue_style( 'wp-color-picker' );
?>
<input name="mv_cr_section_color" type="text" id="mv_cr_section_color" value="#ffffff" data-default-color="#ffffff">
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#mv_cr_section_color').wpColorPicker();
});
</script>
<?php
}
When using admin_enqueue_scripts, the callback function has one parameter $hook_suffix. With it, you can make sure the scripts and styles are only added in the correct screen:
add_action( 'admin_enqueue_scripts', 'b5f_custom_enqueue' );
function b5f_custom_enqueue( $hook_suffix )
{
// CHECK IF CORRECT PAGE, IF NOT DO NOTHING
# if ( 'my_hook-name' != $hook_suffix )
# return;
?>
<script type="text/javascript">
// Use this to check the hook_suffix name
console.log('<?php echo $hook_suffix; ?>');
</script>
<?php
}
Just include a jQuery file and stylesheet file by this script.
// Register Scripts & Styles in Admin panel
function custom_color_picker_scripts() {
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_script( 'iris', admin_url( 'js/iris.min.js' ), array( 'jquery-ui-draggable', 'jquery-ui-slider', 'jquery-touch-punch' ), false, 1 );
wp_enqueue_script( 'cp-active', plugins_url('/js/cp-active.js', __FILE__), array('jquery'), '', true );
}
add_action( 'admin_enqueue_scripts', custom_color_picker_scripts);
Now create a new javascript file as like cp-active.js and keep it avobe defined “/js/cp-active.js” file path using bellows code.
jQuery('.color-picker').iris({
// or in the data-default-color attribute on the input
defaultColor: true,
// a callback to fire whenever the color changes to a valid color
change: function(event, ui){},
// a callback to fire when the input is emptied or an invalid color
clear: function() {},
// hide the color picker controls on load
hide: true,
// show a group of common colors beneath the square
palettes: true
});
Add a textbox to your settings page with a CSS class for the color picker, where you want to display the input text. I have use “color_code” for input $variable.
<input id="color_code" class="color-picker" name="color_code" type="text" value="" />
Please see more details on Add jQuery Color Picker WordPress Theme or Plugin

Categories