So what I'm trying to do is to make a plugin where the user can input anything in a input field and then it uses the given "uid" to make a short code.
So when i type in [text_field_1] somewhere, it'll refer to what is written in that field.
So there's a fixed amount of fields that i determine, so it doesn't need to generate new fiels and so on, so far it works except for the shortcode part, i have no idea where to go, I've tried a few different solutions, but without luck. Kept giving me errors... :) I'm also very very new to this, so this might be way over thought.
Appreciate any help out there!
<?php
/**
* Plugin Name: Custom Shortcodes
* Description: Create custom shortcodes for use in wordpress
* Version: 0.1
**/
class custom_shortcode {
public function __construct() {
// Hook into the admin menu
add_action( 'admin_menu', array( $this, 'create_plugin_settings_page' ) );
// Add Settings and Fields
add_action( 'admin_init', array( $this, 'setup_sections' ) );
add_action( 'admin_init', array( $this, 'setup_fields' ) );
}
public function create_plugin_settings_page() {
// Add the menu item and page
$page_title = 'Custom Shortcode Settings Page';
$menu_title = 'Custom Shortcode';
$capability = 'manage_options';
$slug = 'Custom_shortcode';
$callback = array( $this, 'plugin_settings_page_content' );
$icon = '';
$position = 100;
add_menu_page( $page_title, $menu_title, $capability, $slug, $callback, $icon, $position );
}
public function plugin_settings_page_content() {?>
<div class="wrap">
<h2>Custom Shortcodes</h2><?php echo do_shortcode("[text_field_1]");
if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] ){
$this->admin_notice();
} ?>
<form method="POST" action="options.php">
<?php
settings_fields( 'custom_shortcode' );
do_settings_sections( 'custom_shortcode' );
submit_button();
?>
</form>
</div> <?php
}
public function admin_notice() { ?>
<div class="notice notice-success is-dismissible">
<p>Dine ?ndringer er nu blevet opdateret!</p>
</div><?php
}
public function setup_sections() {
add_settings_section( 'section_1', 'Indtast dine shortcodes her', array( $this, 'section_callback' ), 'custom_shortcode' );
add_settings_section( 'section_2', '', array( $this, 'section_callback' ), 'custom_shortcode' );
}
public function setup_fields() {
$fields = array(
array(
'uid' => 'text_field_1',
'label' => 'Firmanavn',
'section' => 'section_1',
'type' => 'text',
'placeholder' => 'Indtast her',
),
array(
'uid' => 'text_field_2',
'label' => 'test',
'section' => 'section_2',
'type' => 'text',
'placeholder' => 'Indtast her',
)
);
foreach( $fields as $field ){
add_settings_field( $field['uid'], $field['label'], array( $this, 'field_callback' ), 'custom_shortcode', $field['section'], $field );
register_setting( 'custom_shortcode', $field['uid'] );
}
}
public function field_callback( $arguments ) {
$value = get_option( $arguments['uid'] );
if( ! $value ) {
$value = $arguments['default'];
}
switch( $arguments['type'] ){
case 'text':
printf( '<input name="%1$s" id="%1$s" type="%2$s" placeholder="%3$s" value="%4$s" /> <input value="[%1$s]" readonly />', $arguments['uid'], $arguments['type'], $arguments['placeholder'], $value );
break;
}
}
}
new custom_shortcode();
?>