Wordpress - How to pass data to shortcode via form - php

I have custom post type called "forms", and has a sub-menu page which contains form, to add a name.
add_action('admin_menu', 'form_builder_add');
function form_builder_add() {
//create new top-level menu
add_submenu_page('edit.php?post_type=forms', 'Add name', 'Add name', 'manage_options', 'form-dashboards', 'add_names');
}
function add_names() {
echo
'
<form method="post" action="">
<input type="text" name="name" id="name" />
<input type="submit" value="submit" />
</form>
';
}
and I have shortcode and foocode where I want to spit out the values from the form.
add_shortcode( 'foocode', 'prefix_foocode' );
function prefix_foocode() {
return 'This is '. $_POST['name'] . ' testing';
}
So when I call the shortcode from the post, it should have the value of name from the form.
[foocode]
This is example name testing.

Related

wordpress admin panel get parameter

I'm developing a plugin in wordpress, there is data that needs to be listed by search but I can't get them with the get parameter.
I can send with the post parameter, but when the user refreshes the page, she has to search again according to the order number.
Following my code:functions.php
add_action('admin_menu', 'testPluginAdminMenu');
function testPluginAdminMenu()
{
add_menu_page('Return Request',
'Return Request',
'manage_options',
'list',
'myFunction'
);
add_submenu_page(
'null',
'Return Request List',
'Return Request List',
'manage_options',
'listAll',
'myFunctionList');
}
index.php
<?php
function myFunctionList(){
if(isset($_GET['request_order'])){
echo $search = $_GET['request_order'];
}
}
function myFunction(){ ?>
<form method="get" action="<?php echo admin_url('admin.php?page=listAll&request_order='.$_GET['request_order'] ) ?>">
<input type="text" name="request_order" placeholder="Search Order Number..">
<button type="submit" >Search</button>
</form>
<?php } ?>
Output from url: localhost/wordpress/wp-admin/admin.php?request_order=7481
page=listAll not appearing on url
Thank you advance.
Can you try something like this
$qs = array(
'page' => 'listAll',
'request_order' => $_GET['request_order']
);
$qs = http_build_query($qs, null, "&", PHP_QUERY_RFC3986);
<?php echo admin_url('admin.php?' . $qs ) ?>
UPDATE 1
<form method="get" action="<?php echo admin_url('admin.php?page=listAll&request_order='.$_GET['request_order'] ) ?>">
<input type="text" name="request_order" placeholder="Search Order Number..">
<input type="hidden" name="page" value="listAll" />
<button type="submit" >Search</button>
</form>

How to enable/disable custom meta-box in WordPress?

I want to enable and disable custom meta-box on click of a button in my plugin. This button will be on the settings page of the plugin. This is what I've done so far:
function wpplugin_settings_page_markup()
{
if(!current_user_can('manage_options'))
{
return;
}
?>
<div class="wrap">
<h1><?php esc_html_e(get_admin_page_title()); ?></h1>
<?php
?>
<form method="post" action="">
<input type="checkbox" id="postcb" name="postcheck" value="Post">
<label id='postcbid' name="labelpostcheck" for='postcb'>
Post
</label>
<input type="submit" value="submit" name="submit_btn">
</form>
</div>
<?php
function cd_meta_box_add()
{
$multi_posts=array('post', 'page');
foreach ($multi_posts as $multi_post) {
add_meta_box(
'my-meta-box-id', //id
'Custom Meta Box', //title
'cd_meta_box_cb', //callback
$multi_post, //post type
'normal', //position
'high' //priority
);
}
}
add_action('add_meta_boxes', 'cd_meta_box_add');
function cd_meta_box_cb()
{
echo'<b> This is Custom meta box </b>';
}
}
This code does not display the meta-box but I want the meta-box to be added only when that checkbox is checked, and removed when checkbox is not checked. Can anyone please help me achieve that? Thanks!

error with submitting a form in wordpress

i've created a plugin (simple form) and this is the shortcode.php file:
<?php
add_shortcode('contact_form','contact_form');
function contact_form(){
if (isset($_POST['submit'])){
global $wpdb, $table_prefix;
$name = $_POST['name'];
$data = array('name'=>$name,'message'=>'message');
$wpdb->insert($table_prefix.'contact_form',$data,array('%s','%s'));
echo 'added';
}
?>
<form method="POST" action="" id="contact_form">
Name: <input type="text" name="name"><br>
<input type="submit" name="submit" value="submit">
</form>
<?php
}
when I submit the form, if I write sth in the text field, is says 'The page doesn't exist' but if i leave it empty, it submits the form.
what is the problem?
I have used this shortcode in a page.
See this question on the WordPress StackExchange site. In summary, WordPress gets confused by a field called name. Try renaming it to (say) contact_name.
Loads more detail here (also wpse).

Stay on same page after form submit within WordPress

I've added my HTML page as part of the admin wordpress (see screenshot)
And I'm trying to get it so on each submit button, if the record is successfully added to the database a pop up shows up saying "Success!" and then clear the form data without leaving the page. At the moment my current set up tries to load the external page instead of remaining on the page in the screenshot. Here's my HTML and PHP:
<form class="pure-form" name="addAthlete" action="submitForm.php" method="POST">
<fieldset class="pure-group">
<input type="text" class="pure-input-2" name="membershipID" placeholder="Membership ID">
<input type="text" class="pure-input-2" required="required" name="firstName" placeholder="First Name">
<input type="text" class="pure-input-2" required="required" name="surname" placeholder="Surname">
</fieldset>
<button name="submitAthlete" type="submit" class="pure-button pure-input-2 pure-button-primary">Submit</button>
</form>
<?php
function showSuccess() {
echo "Success! One record added.";
}
if (isset($_POST['submitAthlete'])) {
addAthlete();
}
function addAthlete() {
include 'addAthlete.php';
showSuccess();
}
?>
I'm assuming the problem lies with the fact that the echo "Success" is trying to echo on the submitForm.php page which is how I've written it. This is because of the way the page is embedded into wordpress, you can see this below:
add_action( 'admin_menu', 'db_menu' );
function db_menu() {
add_menu_page(
'Database Form', // page title
'Database Form', // menu title
'manage_options', // capability
'database-form', // menu slug
'wpse_91693_render' // callback function
);
}
function wpse_91693_render() {
global $title;
print '<div class="wrap">';
print "<h1>$title</h1>";
$file = plugin_dir_path( __FILE__ ) . "submitform.php";
if ( file_exists( $file ) )
require $file;
print '</div>';
}
How can I get a pop up or something to show up within this WordPress page on each submit?
Thanks!
When you submit a form, it posts data to a new webpage by loading it. In order to stay on the same page, the action should take you to that same page ( move your processing logic there aswell, checking for posted arguments ) or if you don't want to see a reload, use ajax instead.

Wordpress Theme Options field not retaining value

I'm having what im sure is a simple issue but i can't manage to figure it out.
I'm coding a theme options page for my wordpress template and i've managed to get it where the values are saved and i can use them on the site but whenever i reload the theme options page all the form fields are blank and any previously applied settings are gone. My code is below.
<?php
//Theme Options Functionality is Below
if (get_option('pardue_theme_options')){
$theme_options = get_option('pardue_theme_options');
} else {
add_option('pardue_theme_options', array(
'sidebar2_on' => true,
'footer_text' => 'Made by William'
));
}
?>
<?php add_action('admin_menu', 'theme_page_add');
function theme_page_add() {
add_submenu_page('themes.php', 'Pardue Theme Options', 'Theme Options', 'administrator', 'themeoptions', 'theme_page_options');
}
function theme_page_options() {
global $theme_options;
$new_values = array(
'footer_text' => htmlentities($_POST['footer_text'], ENT_QUOTES),
);
update_option('pardue_theme_options', $new_values);
$theme_options = get_option('pardue_theme_options');
echo '<div class="wrap">';
echo '<h2>Theme Options</h2>';
?>
<form action="themes.php?page=themeoptions" method="post">
<label for="footer_text">Footer Text: </label><input name="footer_text" id="footer_text" value="<?php echo $theme_options['footer_text']; ?>" /> <br /> <br />
<label for="sidebar_checkbox">Sidebar 2 on: </label> <input name="sidebar_checkbox" id="sidebar_checkbox" value="on" type="checkbox" /> <br /> <br />
<input type="submit" value="Update Options" name="submit" />
</form>
<?php
echo '</div>';
}
?>
I found a good solution for coding my theme options. The plugin at the link below makes it very easy to code up theme options.
Link to plugin
Documentation is included when you activate the plugin.

Categories