How to add custom metaboxes to taxonomies? - php

So I know how to save things when a taxonomy is edited like this:
if( !function_exists('mytaxonomysave') ):
function mytaxonomysave($term_id, $tt_id, $taxonomy) {
if($taxonomy == 'series'){
if(isset($_POST['type'])){
update_term_meta($term_id, 'type', $_POST['type']);
}
}
}
endif;
add_action( 'edit_term', 'mytaxonomysave', 10, 3 );
But I have no idea how to add a the metabox to the proper areas, what can I do? whats the correct hook¿

The hook you're looking for is: {taxonomy}_add_form_fields so in your case you use:
function series_types() {
?>
<div class="form-field term-description-wrap">
<label for="tag-type">Type</label>
<input type='text' name='tag-type'></input>
</div>
<?php
}
endif;
add_action('series_add_form_fields', 'series_types');

Related

How to call a php class in wordpress?

I have a nice demo importer which is working fine but with a category post count issue so there is plugin which is fixing this and I'm trying to integrate into my demo importer library.
This plugin here is fixing the issue after demo import but requires to go into the plugin page selecting post types in order to click on submit button.
Well this is the plugin admin page:
<?php
if($_POST) {
if(!check_admin_referer('select_post_types_'.get_current_user_id() )){
echo 'That is not allowed'; exit;
}
$fixes = $_POST['fix'];
foreach($fixes AS $fix_id){
$fix = new Fix_Category_Count();
$fix->post_type = $fix_id;
if($fix->process()){
$updated = TRUE;
}
}
}
?>
<div>
<?php echo "<h2>" . __( 'Fix Category Count Settings', 'fix_category_count' ) . "</h2>"; ?>
<h3>Select Post Types to Fix</h3>
<form name="site_data_form" method="post" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>">
<div class="sa_block">
<?php
$post_types = get_post_types(array('public'=>TRUE));
foreach($post_types AS $post_type){
?>
<input type="checkbox" class="post_type" name="fix[]" id="fix_<?=$post_type; ?>" value="<?=$post_type; ?>" /> <label for="fix_<?=$post_type; ?>"><?=ucwords(preg_replace("/_/"," ",$post_type)); ?> (<?=$post_type;?>)</label><br />
<?php
}
?>
<br><br>
Select All |
Deselect All
</div>
</div>
<?php wp_nonce_field('select_post_types_'.get_current_user_id()); ?>
<div class="submit">
<input type="submit" name="Submit" value="<?php _e('Fix Categories Now', '' ) ?>" />
</div>
<?php if($updated){ ?>
<div class="updated"><p><strong><?php _e('Categories Updated.' ); ?></strong></p></div>
<? } ?>
</form>
</div>
<script type="text/javascript">
if(jQuery){
jQuery(document).ready(function($){
$('.select_boxes').click(function(e){
e.preventDefault();
if($(this).attr('rel')=='all'){
$('.post_type').each(function() {
this.checked = true;
});
}
else{
$('.post_type').each(function() {
this.checked = false;
});
}
});
});
}
</script>
So the CLASS of the plugin is here
I want to use only the class without the admin page with something like this:
ob_start();
if ( 'complete' == $response['process'] ) {
// Set imported menus to registered theme locations
$locations = get_theme_mod( 'nav_menu_locations' ); // registered menu locations in theme
$menus = wp_get_nav_menus(); // registered menus
if ( $menus ) {
foreach( $menus as $menu ) { // assign menus to theme locations
if( 'Main Menu' == $menu->name ) {
$locations['primary'] = $menu->term_id;
break;
}
}
}
set_theme_mod( 'nav_menu_locations', $locations ); // set menus to locations
global $wp_rewrite;
$wp_rewrite->set_permalink_structure('/%postname%/');
$wp_rewrite->flush_rules();
// I want to add the plugin functionality as a last process here!
}
ob_end_clean();
I'm not a php developer yet but I would like to get this working without fixing the category count using the plugin page options but directly on the function above and with the php class provided by plugin.
Thank you.

WordPress Options Page function visibility

i am currently setting up an options page in wordpress for my plugin. Why is it not possible to implement the functions as protected or private? The class is directly instantiated. As far as is know, it is possible with java as object oriented language to make them protected in this scenario, isn´t it?
Do i have to implement the $this variable generally with array or is this wordpress specific? It works right now and i have found some comments in the doc claiming that it has to me made this way, but i don´t understand why it´s necessary.
I know this is mostly basic php knowledge, but that´s why i am asking - to learn. Thanks for any help
<?php
class SettingsPage {
static $optionGroup = 'juvo_option_group';
static $optionTitle = 'JUVO Anpassungen';
public function __construct() {
// create custom plugin settings menu
add_action('admin_menu', array( $this, 'my_cool_plugin_create_menu'));
//call register settings function
add_action( 'admin_init', array( $this, 'register_juvo_plugin_settings' ));
}
public function my_cool_plugin_create_menu() {
//create new top-level menu
add_options_page(
self::$optionTitle, //Page Title
self::$optionTitle, //Menu Title
'manage_options', //required Capabilities
'juvo-setting', //Slug
array( $this, 'juvo_settings_page')
);
}
public function register_juvo_plugin_settings() {
//register our settings
register_setting( self::$optionGroup, 'privacy_policy_comments' );
}
public function juvo_settings_page() {
?>
<div class="wrap">
<h1><?php echo self::$optionTitle ?></h1>
<form method="post" action="options.php">
<?php settings_fields( self::$optionGroup );
$options = get_option( 'privacy_policy_comments' );
do_settings_sections( self::$optionGroup ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Seitentitel Datenschutzerklärung</th>
<td><input type="text" name="privacy_policy_comments[title]" value="<?php echo esc_attr( $options['title']); ?>" /></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php }
}
if( is_admin() )
$my_settings_page = new SettingsPage();

Default option value not working automatically

Hi expert wp plugin developers, I need help. I have apply bellow code but not working default value and not showing updated notification after clicking save button. When I put all value in option page from dashboard everything is ok. But not showing all updated notification. Please help me.
<?php
function hkhk_options() {
add_menu_page('Scrol Line Admin Settings', 'hk Settings','manage_options',
'hk_settings', 'hk_admin_options');
}
add_action('admin_menu', 'hkhk_options');
function hk_defaults()
{
$hk_options = array(
'back_color' => '#ccc',
);
}
if ( is_admin() ) :
function hk_register_settings () {
register_setting('hkhk_options', 'hk_options', 'hk_validate_options');
}
add_action('admin_init', 'hk_register_settings');
function hk_admin_options() {
global $hk_options;
if ( ! isset( $_REQUEST['updated'] ) )
$_REQUEST['updated'] = false;
?>
<div class="wrap">
<h2>Select Scrol Line Option</h2>
<?php if ( false !== $_REQUEST['updated'] ) : ?>
<div class="update fade"><p><strong><?php _e( 'Options saved' ); ?></strong></p></div>
<?php endif; // If the form has just been submitted, this shows the notification ?>
<form method="post" action="options.php">
<?php $settings=get_option ( 'hk_options', $hk_options ); ?>
<?php settings_fields('hkhk_options'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row"><label for="back_color"> Back Color </label></th>
<td>
<input id="back_color" type="text" name="hk_options[back_color]" value="<?php esc_attr_e($settings['back_color']); ?>" class="wp-picker-container" /><p class="description"> Choose any color from here for background color. </p>
</td>
</tr>
</table>
<p class="submit"><input type="submit" class="button-primary" value="Save Options" /> </p>
</form>
</div>
<?php
}
function hk_validate_options( $input ){
global $hk_options;
$settings = get_option( 'hk_options', $hk_options );
$input['back_color'] = wp_filter_post_kses( $input['back_color'] );
return $input;
}
endif;
function scrol_line_active() {?>
<?php global $hk_options; $hk_settings = get_option ( 'hk_options', $hk_options ); ?>
<script type="text/javascript">
jQuery(document).ready(function($) {
jQuery("body") .hk({
backColor: "<?php echo $hk_settings['back_color']; ?>",
});
});
</script>
<?php
}
add_action ('wp_head', 'scrol_line_active');
?>
You are checking wrong REQUEST parameter. You need to check $_REQUEST['settings-updated'].
When option page is submitted new URL is similar to /wp-admin/admin.php?page=hk_settings&settings-updated=true.
Hope this helps.

WordPress My theme options settings not saving

I just created a simple theme option page that is working fine and also saved after when press save options.
But when I go somewhere else from theme options page and come back to theme options page that settings what I saved just disappear and I have to change that again whenever I come to theme options page.
Here is my code
add_action( 'admin_menu', 'theme_options_add_page' );
if ( get_option('new_theme_options')) {
$theme_options = get_option('new_theme_options');
} else {
add_option('new_theme_options', array (
'sidebar2_on' => true,
'footer_text' => ''
));
$theme_options = get_option('new_theme_options');
}
function theme_options_add_page() {
add_submenu_page( 'themes.php', 'My Theme Options', 'Theme Options', 8, 'themeoptions', 'theme_options_do_page' );
}
function theme_options_do_page() {
global $theme_options;
$new_values = array (
'footer_text' => htmlentities($_POST['footer_text'], ENT_QUOTES),
);
update_option('new_theme_options', $new_values);
$theme_options = $new_values;
?>
<div class="wrap">
<?php screen_icon(); echo "<h2>" . get_current_theme() . __( ' Theme Options', 'responsivetheme' ) . "</h2>"; ?>
<form method="post" action="themes.php?page=themeoptions">
<label for="footer_text">Footer Text:</label>
<input id="footer_text" type="text" name="footer_text" value="<?php echo $theme_options['footer_text']; ?>" />
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e( 'Save Options', 'responsivetheme' ); ?>" />
</p>
</form>
</div>
<?php
}
#Praveen answer is correct, but for completeness I'll post the full code I tested. Please, note that you should always develop with WP_DEBUG enabled. It shows three issues with your code:
using $_POST['footer_text'] without it being defined (Praveen's answer)
using the deprecated function get_current_theme()
using Level instead of Capability in add_submenu_page()
I dropped the following code into my theme's functions.php and it works ok:
if ( get_option('new_theme_options')) {
$theme_options = get_option('new_theme_options');
} else {
$theme_options = array (
'sidebar2_on' => true,
'footer_text' => ''
);
add_option( 'new_theme_options', $theme_options );
}
add_action( 'admin_menu', 'theme_options_add_page' );
function theme_options_add_page() {
add_submenu_page(
'themes.php',
'My Theme Options',
'Theme Options',
'add_users',
'themeoptions',
'theme_options_do_page'
);
}
function theme_options_do_page() {
global $theme_options;
if( isset( $_POST['footer_text'] ) ) {
$new_values = array (
'footer_text' => htmlentities( $_POST['footer_text'], ENT_QUOTES),
);
update_option('new_theme_options', $new_values);
$theme_options = $new_values;
}
?>
<div class="wrap">
<?php screen_icon(); echo "<h2>" . wp_get_theme() . __( ' Theme Options', 'responsivetheme' ) . "</h2>"; ?>
<form method="post" action="themes.php?page=themeoptions">
<label for="footer_text">Footer Text:</label>
<input id="footer_text" type="text" name="footer_text" value="<?php echo $theme_options['footer_text']; ?>" />
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e( 'Save Options', 'responsivetheme' ); ?>" />
</p>
</form>
</div>
<?php
}
The only issue I can see is the option value sidebar2_on that's being overwritten in theme_options_do_page(), but your sample code does not show it being used elsewhere.
Please change this code:
if($_POST['footer_text']) {
$new_values = array (
'footer_text' => htmlentities($_POST['footer_text'], ENT_QUOTES),
);
update_option('new_theme_options', $new_values);
$theme_options = $new_values;
}
hope this will work fine :)

Jquery read <?php echo get_option('value'); ?> and not the value

This is the php function present in function.php :
<?php
add_action('admin_menu', 'baw_create_menu');
function baw_create_menu() {
add_menu_page('Bubbles', 'Bubbles', 'administrator', __FILE__, 'baw_settings_page');
add_action( 'admin_init', 'register_mysettings' );
}
function register_mysettings() {
register_setting( 'baw-settings-group', 'category' );
}
function baw_settings_page() {
?>
<div class="wrap">
<form method="post" action="options.php">
<?php settings_fields( 'baw-settings-group' ); ?>
<?php do_settings_sections( 'baw-settings-group' ); ?>
<textarea style="width:400px" name="category"><?php echo get_option('category'); ?></textarea>
<?php submit_button(); ?>
</form>
</div>
<?php } ?>
And I want to get the string :
var category = "<?php echo get_option('category'); ?>";
alert(category);
But the alert gives me my php code: <php echo get_option ('category');?> And not the string such as "banana"
var category = <?php echo json_encode(get_option('bulle_index_1')); ?> ;
alert( category );
http://www.php.net/json_encode
PHP cannot be parsed in a .js file. You need to create a function in your .js file and pass in the string returned from PHP:
function fromPHP(phpString) {
var category = phpString;
alert(category);
}
In your .php file:
fromPHP(<?php echo json_encode(get_option('bulle_index_1')); ?>);

Categories