Last active
August 19, 2023 00:01
-
-
Save tatianepires/b7964a3e5da951dfc7b12bc5bf059eba to your computer and use it in GitHub Desktop.
Add metabox to Link Manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Add metabox to Link Manager | |
* Description: Add attributes to each link, each link's attributes are saved serialized as an option. | |
* Author: Tatiane Pires | |
* Author URI: http://tatianepires.com.br | |
* Version: 1.0 | |
*/ | |
/* | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
*/ | |
/* | |
* To enable Link Manager add to your functions.php file: | |
add_filter( 'pre_option_link_manager_enabled', '__return_true' ); | |
*/ | |
add_action( 'add_meta_boxes', 'tps_link_metabox' ); | |
/* Do something with the data entered */ | |
add_action( 'add_link', 'tps_link_meta_update_postdata' ); | |
add_action( 'edit_link', 'tps_link_meta_update_postdata' ); | |
/* Adds a box to the mainside column on the Link Manager edit screen */ | |
function tps_link_metabox() { | |
$screens = array( 'link' ); | |
foreach ($screens as $screen) { | |
add_meta_box( | |
'tps_link_box', | |
'Link Attributes', | |
'tps_link_inner_box', | |
$screen, | |
'side', | |
'core', | |
); | |
} | |
} | |
/* Prints the box content */ | |
function tps_link_inner_box( $post ) { | |
// Use nonce for verification | |
wp_nonce_field( plugin_basename( __FILE__ ), 'tps_link_nonce' ); | |
$tps_link_field_option_values = tps_link_get_option(tps_link_get_id()); | |
$link_id = tps_link_get_id(); | |
$is_yes_checked = ($tps_link_field_option_values['title'] === tps_link_get_option_value('YES')) ? 'checked' : ''; | |
$is_no_checked = (!$is_yes_checked) ? 'checked' : ''; | |
$tps_link_field_option_yes = tps_link_get_option_value('YES'); | |
$tps_link_field_option_no = tps_link_get_option_value('NO'); | |
//$tps_link_field_option_order = isset($tps_link_field_option_values['order']) ? intval($tps_link_field_option_values['order']) : 99; | |
$tps_link_field_option_order = $tps_link_field_option_values['order']; | |
echo "<!-- "; | |
echo "tps_link_field_option_values: "; | |
var_dump($tps_link_field_option_values); | |
echo " -->"; | |
echo <<<TPS_LINK_METABOX | |
<div> | |
<div class="inside" style="border-bottom: 1px solid #ccc;"> | |
<strong>Title Link</strong> | |
<p>Display this link at the top of it's category's list?</p> | |
<div> | |
<input type="radio" id="tps_link_field_is_title_yes" name="tps_link_field_is_title" value="$tps_link_field_option_yes" $is_yes_checked /> | |
<label for="tps_link_field_is_title_yes">Yes</label> | |
</div> | |
<div> | |
<input type="radio" id="tps_link_field_is_title_no" name="tps_link_field_is_title" value="$tps_link_field_option_no" $is_no_checked /> | |
<label for="tps_link_field_is_title_no">No</label> | |
</div> | |
</div> | |
<div class="inside"> | |
<label for="tps_link_field_order"><strong>Link Order</strong> | |
<p>Set the order to display the links inside the category.</p> | |
<div> | |
<input type="number" id="tps_link_field_order" name="tps_link_field_order" placeholder="0" value="$tps_link_field_option_order" /> | |
</div> | |
</div> | |
</div> | |
TPS_LINK_METABOX; | |
} | |
function tps_link_meta_update_postdata( $link_id ) { | |
// verify if this is an auto save routine. | |
// If it is our form has not been submitted, so we dont want to do anything | |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) | |
return; | |
// verify this came from the our screen and with proper authorization, | |
// because save_post can be triggered at other times | |
if ( !isset( $_POST['tps_link_nonce'] ) || !wp_verify_nonce( $_POST['tps_link_nonce'], plugin_basename( __FILE__ ) ) ) | |
return; | |
// OK, we're authenticated: we need to find and save the data | |
//sanitize user input | |
$tps_link_data = array( | |
'title' => isset($_POST['tps_link_field_is_title']) ? sanitize_text_field($_POST['tps_link_field_is_title']) : $tps_link_field_options['NO'], | |
'order' => isset($_POST['tps_link_field_order']) ? intval(sanitize_text_field($_POST['tps_link_field_order'])) : 0, | |
); | |
tps_link_update_option($link_id, $tps_link_data); | |
} | |
function tps_link_get_id() { | |
$link_id = null; | |
if (isset($_GET['link_id'])) { | |
$link_id = $_GET['link_id']; | |
} else { | |
global $wpdb; | |
$result = $wpdb->get_results( "SHOW TABLE STATUS LIKE 'wp_links'", ARRAY_A ); | |
$link_id = $result[0]['Auto_increment']; | |
} | |
return $link_id; | |
} | |
function tps_link_option_name($link_id) { | |
return sprintf('tps_link_options_%s', $link_id); | |
} | |
function tps_link_get_option($link_id) { | |
$option = get_option(tps_link_option_name($link_id)); | |
return unserialize($option); | |
} | |
function tps_link_update_option($link_id, $link_data) { | |
$tps_link_option_name = tps_link_option_name($link_id); | |
$tps_link_option_value = tps_link_get_option($link_id); | |
$serialized_data = serialize($link_data); | |
if ($link_option_value === false) { | |
add_option($tps_link_option_name, $serialized_data); | |
} else { | |
update_option($tps_link_option_name, $serialized_data); | |
} | |
} | |
function tps_link_get_option_value($option) { | |
// Prevent that different values might be stored as this field's option | |
$tps_link_field_options = array( | |
'YES' => 'yes', | |
'NO' => 'no', | |
); | |
return $tps_link_field_options[$option]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment