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/
Related
I am trying to modify WordPress search form, so that after user enters the word in the search bar, that word would be translated and accordingly redirected into external link (for example google). I managed to do translation, redirection, but struggle to get search query in first place.
My code:
<form id="myform" name="myform" role="search" method="get" class="search-form" action="" target="_blank" >
<input type="text" id="SearchText" value="" name="SearchText" placeholder="<?php echo esc_attr($search_text) ;?>" >
<button onclick="go()" type="submit" id="searchsubmit" class="btnsearch"></button>
</form>
Then code for translation needs input for search query $XXXXXXXXXXXXX:
$url = 'https://www.googleapis.com/language/translate/v2?key=' . $apiKey . '&q=' . rawurlencode($XXXXXXXXXXXXX) . '&source=xx&target=en';
$str = $responseDecoded['data']['translations'][0]['translatedText'];
Then I have script that would take translated search query $str:
<script type="text/javascript">
function go() {
document.myform.action = "https://www.google.com/";
document.myform.SearchText.value = "<?php echo $str ?>";
...............
}
</script>
Everything works if I put any word instead $XXXXXXXXXXXXX, but the question what here needs to be entered so that it would take original search entry.
Thank you in advance for any help.
Do you need to receive a request passed in a get parameter? You can use jquery $.get https://api.jquery.com/jquery.get/
I am developing a WordPress plugin that is inserted onto the page by adding a token to the page content.
So, on the page there is some introductory text with the contents of the plugin below. On postback, I would like to clear the introductory text and just show output from the plugin.
I know I could do this using jQuery by replacing the contents of $(".entry-content").html("plugin output"); but I wanted to ask if there was a WordPress native method of doing this instead.
UPDATE
The following is one of the files from the plugin. It is on the POST (the if condition) that I want to replace the page content, with the output of the function. On the GET (the else condition) I just want to append the output of the function to the content.
<?php
/*
The following code utilizes Heredoc syntax.
It is very important to note that the line with the closing identifier must contain no other characters, except a semicolon (;).
That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon.
It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system.
This is \n on UNIX systems, including Mac OS X.
The closing delimiter must also be followed by a newline.
*/
class WHRFContactUs {
function GenerateContactUsForm() {
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$sendgrid = new SendGrid($GLOBALS['MailAPIKey']);
$email = new SendGrid\Email();
$email
->addTo($GLOBALS['MailAPISender'])
->setReplyTo($_POST['Email'])
->setFrom($GLOBALS['MailAPISender'])
->setSubject($_POST['Subject'])
->setHtml($_POST['Message'] . '<br /><hr/>' . $_POST['FullName'] . ' ' . '(' . $_POST['Email'] . ')<br/>' . '<br />')
;
try
{
$sendgrid->send($email);
$html = <<<HTML
Your message has been successfully sent. Thank you for taking the time to provide us your feedback.
<br/><br/>
In the event that your feedback requires a response, a representative will contact you as soon as possible.
HTML;
}
catch(\SendGrid\Exception $ex)
{
echo $ex->getCode();
foreach($ex->getErrors() as $er) {
echo $er;
}
}
}
else
{
$html = <<<HTML
<form method="post" id="ContactUsForm" action="{$_SERVER['REQUEST_URI']}">
<div class="form-group">
<label for="FullName" class="sr-only">Your full name</label>
<input type="text" class="form-control" id="FullName" name="FullName" placeholder="Your full name" data-validation-required="Please enter your full name.">
</div>
<div class="form-group">
<label for="Email" class="sr-only">Your email address</label>
<input type="email" class="form-control" id="Email" name="Email" placeholder="Your email address" data-validation-required="Please enter your email address." data-validation-format="Please enter a valid email address.">
</div>
<div class="form-group">
<label for="Subject" class="sr-only">Subject</label>
<input type="text" class="form-control" id="Subject" name="Subject" placeholder="Subject" data-validation-required="Please enter a subject.">
</div>
<div class="form-group">
<label for="Message" class="sr-only">Message</label>
<textarea class="form-control" id="Message" name="Message" placeholder="Your message..." data-validation-required="Please enter a message." rows="4"></textarea>
</div>
<button type="submit" id="ContactUsFormSubmit" name="ContactUsFormSubmit" class="btn btn-primary">Send message</button>
</form>
<script type="application/javascript" src="{$GLOBALS['WHRFPluginPath']}scripts/whrf-contact-us.js"></script>
HTML;
}
return $html;
}
}
add_shortcode('ContactUsForm', array('WHRFContactUs','GenerateContactUsForm'));
?>
As mentioned in the comments, without knowing how that content is being added it isn't really possible to know how to replace it.
However, there's a possibility of achieving that in a very disruptive and ill-advised way:
Chances are that content is being added by using the filter the_content.
So you could disruptively have a high-priority modification for the content and then remove that filter to stop the other content from being added. As follows:
function my_disruptive_filter($content) {
remove_all_filters('the_content');
return 'my custom content';
}
add_filter( 'the_content', 'my_disruptive_filter', -999, 1);
I'm not 100% sure if a this would work, since I've never tried it.
Also remove_all_filters takes a second parameter that's $priority which is optional. You can target all priorities that are lower that the one using with this hook, via a for loop. But I assume without providing that parameter it would just remove all of them.
Warning
The reason that this is very disruptive is that it would prevent any other code from using that filter. Another developer (or even yourself) might want to use that filter later at some point and it won't work and you have no idea why. Could be a very difficult situation to get out of.
Also this might prevent existing plugin theme from adding their content, so if you wind up using and see missing stuff -- the reason could be this.
Note: this is really a hit-or-miss solution because it depends on how that content is being added.
The function the_content() returns the page content, if you want to overwrite this using your own plugin you should remove this line in whatever page you are (usually page.php/single.php in theme dir) with your custom plugin output.
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
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.
I have added within my WordPress 3.1 site, the following code at the bottom of my sidebar.php file:
<div id="search_box">
<form id="searchform" action="http://www.service.com/" method="get" role="search">
<input id="s" type="text" name="s" class="search" value="Search site" size="19" maxlength="80" id="white_box" onfocus="if (this.value=='Search site') this.value = ''"/>
<input id="searchsubmit" type="image" class="submit" value="submit" src="<?php bloginfo( 'template_url' ); ?>/images/search_btn.jpg" />
</form>
</div>
As I have coded this search process myself, when I place a some text within my search text box and press the "Search" button, it looks as if, is is calling the page search.php within my theme and displaying the results.
Questions:
1) where/how does it know when I press the "Search" button to go off and call search.php?
2) if possible only, how can I change it to call a different php file instead of the search.php
Thanks.
Use template filter
add_filter( 'template_include', 'template_include', 10 );
and change the template as
function template_include($template)
{
if(your condition here){
$template = get_template_directory().'/your-template.php';
}
return $template;
}
-1. All requests for a WP site go to a single page that routes them based upon specific criteria. Thus when a search is performed it knows that it is a search and directs to the search.php page.
Specifically it goes to index.php that loads wp-blog-header.php.
-2: Everything you need to know should be right here: http://codex.wordpress.org/Creating_a_Search_Page#Using_the_page.php
Wordpress assumes the request is a search if it sees an 's' GET variable.
You can hook into the template_redirect action (in your theme's functions.php file) and load a different template like so:
add_action('template_redirect', 'my_template_select');
function my_template_select() {
if (is_search()) {
load_template(TEMPLATEPATH . '/foobar.php');
exit;
}
}