Option page not updating multiple records in settings api - wordpress

Hi i am new to wordpress plugin development. i have issues with settings api. please help if you can.
Below is my problem.
The problem i am facing is the last record which is link it is updating in database but the title text is not updating in database.
So please help me find solution. Thank you.
function load_plugin() {
add_settings_section('plugin_main', '<h1>Ticker Settings</h1>', 'plugin_section_text', 'plugin');
add_settings_field('plugin_text_string', 'Title text', 'plugin_setting_string', 'plugin', 'plugin_main');
add_settings_field('post_title_link', 'Link', 'plugin_link_setting', 'plugin', 'plugin_main');
register_setting('plugin_options', 'plugin_options');
register_setting('post_title_link', 'post_title_link');
}
function plugin_section_text() {
echo '<p>Change your post ticker title and give link to.</p>';
}
function plugin_setting_string() {
echo "<input id='plugin_text_string' name='plugin_options' size='40'
type='text' value='" . get_option('plugin_options') . "' />";
}
function plugin_link_setting() {
echo "<input id='post_title_link' name='post_title_link' size='40'
type='text' value='" . get_option('post_title_link') . "' />";
}
add_action('admin_init', 'load_plugin');
function post_ticker_setting() {
?>
<div class="wrap">
<form action="options.php" method="post">
<?php
settings_fields('plugin_options');
settings_fields('post_title_link');
do_settings_sections('plugin');
?>
<input name="Submit" type="submit" value="<?php esc_attr_e('Save Changes'); ?>" />
</form>
</div>
<?php
}

You have to register all settings with register_setting (https://codex.wordpress.org/Function_Reference/add_settings_field). The register_setting lines you show don't correspond to your fields.
Also sections should be declared after the fields are registered (https://codex.wordpress.org/Function_Reference/register_setting)
In your code, the following lines
<?php
settings_fields('plugin_options');
settings_fields('post_title_link');
do_settings_sections('plugin');
?>
don't correspond to the declarations above:
add_settings_section('plugin_main', ...
add_settings_field('plugin_text_string', ...
add_settings_field('post_title_link', ...
Only the 'post_title_link' has the same name. Which is probably why it is the only one updating.

Related

I am developing plugin give following error while activation gives

The plugin generated 2 characters of unexpected output during activation. If you notice “headers already sent” messages, my code is below
<?php
/*
Plugin Name: configuration
Description: example plugin to demonstrate wordpress capatabilities
Author: kailash
License: Public Domain
*/
// run the install scripts upon plugin activation
function test_init() {
if(isset($_POST['save'])) {
$setting=$_POST['setting'];
global $wpdb;
$table_name ="setting";
$wpdb->query("UPDATE $table_name SET setting='$setting' WHERE id=1");
//echo"Setting Saved";
}
?>
<form name="form" id="myform" method="POST">
<input type="radio" name="setting" value="2" <?php if($setting==2){echo 'checked="checked"';}?> id="term"/>newconfiguration<br>
<input type="radio" name="setting" value="1" <?php if($setting==1){echo 'checked="checked"';}?> id="term"/>oldconfiguration<br>
<input type="submit" name="save" value="save">
<?php
}
add_action('admin_menu', 'config_plugin_setup_menu');
function config_plugin_setup_menu(){
add_menu_page( 'Test Plugin Page', 'configuration', 'manage_options', 'config-plugin', 'test_init' );
}
Headers already sent
It is usually because there are spaces, new lines, or other garbage before an opening <?php tag or after a closing ?>
check and remove all unnecessary space. example check before
<form> tag. means after ?> tag there is extra space in your code. so check other too and remove all space..!
Check that the very first characters are
<?php
Check that the very last characters are
?>
for details check this
https://codex.wordpress.org/Answers-Troubleshooting

PHP Keeps Duplicating Table Elements in WordPress Theme

I'm working on this test site, which is a WordPress website (using version 5.5) and using the Canvas WooTheme. The front page of it is supposed to display today's calendar events, which are provided by the WordPress plugin, Events Manager.
What I would like for them to do is display, such as this events page (and the screenshot below - seen in this screenshot), does. Instead, the code below somehow has the rest of the template (including the sidebar) sucked into the table, continuing to duplicate table rows and columns into itself (as seen from this screenshot).
The code it shoots out into Firebug is the following:
<table cellspacing="0" cellpadding="0" id="current-events"><tbody><tr><td class="startdate_cost_td">
<span class="start_time"><table cellspacing="0" cellpadding="0" id="current-events"><tbody><tr>
<td class="startdate_cost_td">
<span class="start_time">12:00 AM</span><br>
<span class="cost">Free</span>
</td>
<td class="event_image"> <img src="../../../wp-includes/images/default_img.jpg"></td>
<td class="event_title">Sex Addicts Anonymous meeting<br>
<i><span class="location_name"># No location listed</span></i>
<div id="read_more_wrapper">
<img src="../../../wp-includes/images/little_pink_arrow.jpg"><a class="pink_link" href="http://testbuzz.illinimedia.com/events/sex-addicts-anonymous-meeting-90">READ MORE</a></div></td></tr>
<!--12:00 AM</span><br/><span class='cost'>Free</span></td><td class='event_image'><img src='../../../wp-includes/images/default_img.jpg' /></td><td class='event_title'>Sex Addicts Anonymous meeting<br/><i><span class='location_name'>#No location listed</span></i><br />Mind, body, & spirit<div id='read_more_wrapper'><img src='../../../wp-includes/images/little_pink_arrow.jpg' /><a href='http://testbuzz.illinimedia.com/events/sex-addicts-anonymous-meeting-90' class='pink_link'>READ MORE</a></td></tr></div><tr><td class='startdate_cost_td'>
<span class='start_time'>-->
<tr>
<td class="startdate_cost_td">
<span class="start_time">8:30 AM</span><br>
<span class="cost">Free</span>
</td>
<td class="event_image"><img alt=""Inside India" with Larry Kanfer" src="http://testbuzz.illinimedia.com/wp-content/uploads/locations-pics/event-35214.jpg"> </td>
<td class="event_title">“Inside India” with Larry Kanfer<br>
<i><span class="location_name"># Alice Campbell Alumni Center</span></i>
<div id="read_more_wrapper">
<img src="../../../wp-includes/images/little_pink_arrow.jpg"><a class="pink_link" href="http://testbuzz.illinimedia.com/events/inside-india-with-larry-kanfer-31">READ MORE</a></div></td></tr>
......more events similar to this......
<div class="fix"></div>
<div class="nav-entries">
<span class="nav-prev icon fl">Older posts</span> <div class="fix"></div>
</div>
<!-- /#main -->
<div id="sidebar">
<div class="widget widget_woo_search" id="woo_search-3"><div class="search_main">
<form action="http://testbuzz.illinimedia.com/" class="searchform" method="get">
<input type="text" onblur="if (this.value == '') {this.value = 'Search...';}" onfocus="if (this.value == 'Search...') {this.value = '';}" value="Search..." name="s" class="field s">
<input type="image" name="submit" class="submit" alt="Search" src="http://testbuzz.illinimedia.com/wp-content/themes/canvas/images/ico-search.png">
</form>
<div class="fix"></div>
</div>
.... the rest of the content stuck .....
</span></td></tr></tbody></table>
Below is the code I stuck into our magazine template within our child theme, template-magazine.php, which is causing the problem:
<?php
$page = ( !empty($_REQUEST['page']) && is_numeric($_REQUEST['page']) )? $_REQUEST['page'] : 1;
$args = array(
'limit' => 10,
'scope' => 'today',
'pagination' => 1
);
if (isset($_REQUEST['c'])) {
$args['category'] = $_REQUEST['c'];
$args['scope'] = 'future';
}
$args['offset'] = $args['limit'] * ($page-1);
$events = em_get_events_array($args);
$size = sizeof($events)-1;
echo "<h2 id='headding'class='title'>Today's Events</h2>";
echo "<table cellpadding='0' cellspacing='0' id='current-events' >";
for ($i=0;$i<$size;$i++) {
echo "<tr><td class='startdate_cost_td'>
<span class='start_time'>" . $events[$i][0] .
"</span><br/><span class='cost'>" . $events[$i][7] .
"</span></td><td class='event_image'>";
if ($events[$i][8] == '') {
echo "<img src='../../../wp-includes/images/default_img.jpg' />";
}
else {
echo $events[$i][9];
}
echo "</td><td class='event_title'>" . $events[$i][10] .
"<br/><i><span class='location_name'>#". $events[$i][11] .
"</span></i><br />" .$events[$i][12] .
"<div id='read_more_wrapper'>" .
"<img src='../../../wp-includes/images/little_pink_arrow.jpg' /><a href='" . $events[$i][13] . "' class='pink_link'>READ MORE</a></td></tr>" .
"</div>";
}
echo "</table>";
$last_index = count($events) - 1;
echo $events[$last_index][0];
?>
I made sure the PHP code above was the cause of the main problems, by removing the code itself. That seems to do the trick! When I tried placing this code in different parts of the template-magazine.php file, it repeated the same results. When I also moved this to the functions.php file in our child theme, it simply made everything else go further down and expand to the whole width of the browser. How do I get rid of the looping in the code, having all of the side content being sucked into the table being created?
UPDATE: I commented out the above PHP code using HTML tags, duplicating the events and commenting out the table elements, which we would like to have show on the page. Below is the screenshot of what it looks like: https://docs.google.com/open?id=0B2UC67NQiW2yalhMSGxra0FFWGM and the code that's showing up in the picture below (I didn't want to paste in the code because it would've been very long:
It ended up being that it seemed to be reading in the Event Preview settings, which I created within the plugin. All I needed to do was simply create a for loop, reading in each of the events for that specific page, blocking out the rest of the PHP code above. The following is the code used to read in the events:
for ($i=0;$i<$size;$i++) {
echo $events[$i][0];
}
Hopefully this helps someone using the Events Manager plugin.

Wordpress ajax contact form doesn't return data

I'm making a WordPress theme where every post will have a contact form. The contact form will collect information about the visitor and send a mail to the site administrator. It will also send an email to the visitor with a link to a downloadable PDF (this code is not done yet).
The problem I have is that the php function that handles the ajax form doesn't respond. It only returns a -1 (No Properties). But I get a status code 200 OK.
I'm running the dev server on MAMP Pro OSX and DynDNS.
This is the PHP I have in functions.php
function ajax_contact() {
if(!empty($_POST)) {
$name = $_POST['name'];
$company = $_POST['company'];
$mail = $_POST['mail'];
$admin_mail = get_bloginfo('admin_email');
$error = "";
if(!$name) {
$error .= "Please tell us your name<br/>";
}
if(!$company) {
$error .= "Please tell us your company<br/>";
}
if(!$mail) {
$error .= "Please tell us your E-Mail address<br/>";
}
if(empty($error)) {
$subject = "New download notification";
$message = "You've received a new download notification. \n\n
Name: ".$name."\n
Company: ".$company."\n
Mail: ".$mail."\n";
// Send a mail to the admin with the contact info.
$send_mail = wp_mail($admin_mail, $subject, $message);
// Send mail to visitor with the download link.
if($send_mail) {
echo "sent";
die();
}
} else {
echo "error: " . $error;
die();
}
}
}
add_action('wp_ajax_nopriv_ajax_contact', 'ajax_contact');
add_action('wp_ajax_ajax_contact','ajax_contact');
This is the javascript code
function sendForm(formId, postId, str)
{
console.log("formId: " + formId);
console.log("formId: " + postId);
console.log("str: " + str);
$.ajax({
type: "POST",
url: "../wp-admin/admin-ajax.php",
action: "ajax_contact",
data: str,
success: function(data) {
$("#" + formId).ajaxComplete(function(event, request, settings){
if(data == 'sent') {
$("#" + formId).find(".success").fadeIn("slow");
}
else {
result = data;
$("#" + formId).siblings(".success").html(result);
}
});
}
});
}
And this is the actual form (I have multiple forms on the same page)
<form name="form-<?php the_ID(); ?>" id="form-<?php the_ID(); ?>"/>
Name <br/>
<input type="text" name="name" value=""/><br />
Company / Organisation <br/>
<input type="text" name="company" value=""/><br />
Email <br/>
<input type="text" name="mail" value=""/><br />
<input type="hidden" name="postId" value="<?php the_ID(); ?>">
<input type="submit" value="submit" class="requestbutton" id="requestButton-<?php the_ID(); ?>" rel="<?php the_ID(); ?>"/>
</form>
EDIT: Changed the ajax action name, problem still persists
Your AJAX action: should be contact_form not ajax_contact

posting a footer contact form to a different script not working

I have two files the one which hosts my actual contact form and then a file where i post the form to.
contactform.php (which is part of the footer template)
<form id="contact" action="<?php bloginfo('template_url'); ?>/sendmail.php" method="post">
<label for="name">Your name: *</label>
<input type="text" id="nameinput" name="name" value=""/>
<label for="email">Your email: *</label>
<input type="text" id="emailinput" name="email" value=""/>
<label for="comment">Your message: *</label>
<textarea cols="20" rows="7" id="commentinput" name="comment"> </textarea><br />
</form>
sendmail.php
<?PHP
if(isset($_POST['submit'])) {
error_reporting(E_NOTICE);
function valid_email($str)
{
return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*#([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
}
if($_POST['name']!='' && $_POST['email']!='' && valid_email($_POST['email'])==TRUE && strlen($_POST['comment'])>1)
{
$to = preg_replace("([\r\n])", "", hexstr($_POST['receiver']));
$from = preg_replace("([\r\n])", "", $_POST['email']);
$subject = "Website contact message from ".$_POST['name'];
$message = $_POST['comment'];
$match = "/(bcc:|cc:|content\-type:)/i";
if (preg_match($match, $to) ||
preg_match($match, $from) ||
preg_match($match, $message)) {
die("Header injection detected.");
}
$headers = "From: ".$from."\r\n";
$headers .= "Reply-to: ".$from."\r\n";
if(wp_mail($to, $subject, $message, $headers,'',true))
{
echo 1; //SUCCESS
}
else {
echo 2; //FAILURE - server failure
}
}
else {
echo 3; //FAILURE - not valid email
}
}else{
die("Direct access not allowed!");
}
function hexstr($hexstr) {
$hexstr = str_replace(' ', '', $hexstr);
$hexstr = str_replace('\x', '', $hexstr);
$retstr = pack('H*', $hexstr);
return $retstr;
}
?>
The issue is that this does not know of wp_mail function. I know that I need to include something so wp_mail will be available but what do I add? The function does exist. The issue with including the file that has wp_mail defined is that inside that function it requires some core php functions (wp_mail is being overwritten by cimy_swift plugin)
hi why not try just submitting the form to the base wpurl? then within your header.php file copy and paste your code in?
ie: using a hidden field you can check to see if its been posts, in this case the hidden field is called 'action' and it has a value of 'sendemail'.
form
<form id="contact" action="<?php bloginfo('wpurl'); ?>" method="post">
//form stuff
<input type="hidden" name="action" value="sendemail" />
</form>
Header.php
within the header file we do a call to check and see if the form has been posted,
<html>
<head>
<title><?php wp_title();?></title>
<?php
if( isset($_POST['action']) && ($_POST['action']=='sendemail') ) {
// run your code
}
?>
</head>
if you dont want to go down that route, and wish to use your theme folder to hold the php script then what to is, include the below in your sendmail.php file
define('WP_USE_THEMES', FALSE);
require('../../../wp-blog-header.php');
//above is assuming your file is located in the theme root, not a sub folder.
this will give you access to all the wordpress functions and shortcodes etc..etc..
hope that helps a little..
Marty

Wordpress Widget options saving doesn't work

I want to have changeable widget title via Options Panel in wp-admin > Appearance > Widgets.
It doesn't seem to work, after clicking "SAVE" it always gives back the default instead of saving stuff.
Widgets control panel is very simplistic:
function myplugin_control() {
echo '<p>
<label for="myplugin_title">Title:</label>
<input id="myplugin_title" name="myplugin_title" type="text" value="Default title:"/>
</p>
<p>
<label for="myplugin_number">Number of items to show:</label>
<input id="myplugin_number" name="myplugin_number" type="text" value="5" size="3"/>';
$myplugin_title = ($_POST["myplugin_title"]);
$myplugin_number = ($_POST["myplugin_number"]);
update_option('myplugin_widget', $myplugin_number , $myplugin_title);
}
And plugin goes like:
(...)
function widget_myplugin($args) {
extract($args);
echo $before_widget;
echo $before_title . $myplugin_title . $after_title;
myplugin();
echo $after_widget;
}
I think you're using update_option(); improperly. It only takes two values. http://codex.wordpress.org/Function_Reference/update_option
Try change the name of your title field to simply "title". I think WP looks for this by default; see: http://wordpress.org/support/topic/how-can-i-set-a-widgets-title-in-for-use-in-the-dashboard
Instead of using $_POST['title'], use the more standard $this->get_field_id('title'); and echo $this->get_field_name('title');
Hope this helps! Also: you may find the following link helpful: http://wpengineer.com/1023/wordpress-built-a-widget/

Resources