Redirect after plugin activation - wordpress

How do I redirect users to my plugin settings page after they activate my plugin, I tried
register_activation_hook(__FILE__,'activate_myplugin');
function activate_myplugin()
{
//create and populate DB tables
wp_redirect(plugin_setting_url);
}
but it does not work.

You should be able to do something like this:
register_activation_hook(__FILE__, 'my_plugin_activate');
add_action('admin_init', 'my_plugin_redirect');
function my_plugin_activate() {
add_option('my_plugin_do_activation_redirect', true);
}
function my_plugin_redirect() {
if (get_option('my_plugin_do_activation_redirect', false)) {
delete_option('my_plugin_do_activation_redirect');
wp_redirect(MY_PLUGIN_SETTINGS_URL);
}
}

This will redirect to option page only if that plugin is activated only without using bulk activation mode .
register_activation_hook(__FILE__, 'my_plugin_activate');
add_action('admin_init', 'my_plugin_redirect');
function my_plugin_activate() {
add_option('my_plugin_do_activation_redirect', true);
}
function my_plugin_redirect() {
if (get_option('my_plugin_do_activation_redirect', false)) {
delete_option('my_plugin_do_activation_redirect');
if(!isset($_GET['activate-multi']))
{
wp_redirect("options-general.php?page=your-plugin-option-page");
}
}
}

thanks for your code - it´s great, but only has one downside: upon bulk activation of plugins, you also get redirected to your defined redirect page - which might confuse user when deactivating/activating all plugins at once for test/debug reason. I therefore would propose the solution, to add an option to only redirect to your page on FIRST plugin activation:
register_activation_hook(__FILE__, 'my_plugin_activate');
add_action('admin_init', 'my_plugin_redirect');
function my_plugin_activate() {
add_option('myplugin_redirect_on_first_activation', 'true');
}
function my_plugin_redirect() {
if (get_option(MYPLUGIN_REDIRECT_ON_FIRST_ACTIVATION_KEY) == 'true') {
update_option(MYPLUGIN_REDIRECT_ON_FIRST_ACTIVATION_KEY, 'false');
wp_redirect(MY_PLUGIN_SETTINGS_URL);
}
}

Don't worry it's very simple.
Simply paste this code in you plugin.php file
function_activation_redirect( ) {
exit( wp_redirect( 'http://45.118.207.78/amarwp/wp-admin/admin.php?page=custompage' ) )
}
add_action( 'activated_plugin', 'funtion_activation_redirect' );
http://45.118.207.78/amarwp/wp-admin/admin.php?page=custompage
in my case this is the path of my page where I want to redirect my page.

Hello i have used bellows code redirect after plugin activation. You can use this code. It's working nicely.
register_activation_hook(__FILE__, 'nht_plugin_activate');
add_action('admin_init', 'nht_plugin_redirect');
function nht_plugin_activate() {
add_option('nht_plugin_do_activation_redirect', true);
}
function nht_plugin_redirect() {
if (get_option('nht_plugin_do_activation_redirect', false)) {
delete_option('nht_plugin_do_activation_redirect');
if(!isset($_GET['activate-multi']))
{
wp_redirect("edit.php?post_type=headline&page=news-headline");
}
}
}
nht_ is my plugin prefix & "edit.php?post_type=headline&page=news-headline" is redirect page. please replace this those.

Related

Isse with a user forwarding function in Wordpress

I have created this code for my Wordpress Theme's function.php file. I want to redirect the user to certain URL if they are on a certain page.
Please check if this function is correct? The code is working, but it's creating an error with Woocommerce checkout and not letting customer place an order.
function user_login_check()
{
if (is_user_logged_in()) {
if (is_page(123)) {
wp_redirect('https://example.com/alpha');
exit;
};
if (is_page(456)) {
wp_redirect('https://example.com/beta');
exit;
}
}
}
add_action('wp', 'user_login_check');

TinyMCE - Custom plugin makes visual editor blank in wordpress

I am trying to add TinyMCE custom plugin in wordpress that change the direction of selected text by using <bdo>. I register the plugin in wordpress:
/*Register Custom TinyMCE plugin*/
add_filter('mce_external_plugins', 'my_tinymce_plugins');
function my_tinymce_plugins() {
$plugins_array = array(
'tiny' => 'tiny.js' //Plugin directory is same as theme's funtion.php
);
return $plugins_array;
}
But it hide visual editor completely & also make text editor, un-editable. What is wrong?
I think you're overwriting all the other plugins, rather than just adding yours. Try
function my_tinymce_plugins($plugin_array) {
$plugin_array['tiny'] = 'tiny.js';
return $plugin_array;
}
instead. You may need to prepend get_stylesheet_directory_uri() to tiny.js to ensure the URL is correct.
Edit
Further to your comment, here's some code I used a few years ago to add a button. I can't say for certain best practices haven't changed, but it worked for me:
add_action('init', 'immo_add_column_button');
function immo_add_column_button() {
if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
return;
}
if ( get_user_option('rich_editing') == 'true') {
add_filter('mce_external_plugins', 'immo_add_column_tinymce_plugin');
add_filter('mce_buttons', 'immo_register_addcolumn_button');
}
}
function immo_register_addcolumn_button($buttons) {
array_push($buttons, "|", "addcol");
return $buttons;
}
function immo_add_column_tinymce_plugin($plugin_array) {
$plugin_array['addcol'] = get_bloginfo('stylesheet_directory').'/js/immo_column_button.js';
return $plugin_array;
}
add_filter( 'tiny_mce_version', 'immo_refresh_mce');
function immo_refresh_mce($ver) {
// Force refresh of TinyMCE cache by updating the version number
$ver += 3;
return $ver;
}

How to show text in a page on Wordpress whithin a plugin

I am developing a plugin for a website.(Which is my first plugin for Wordpress)
The basic functionality is querying the database and in specific pages show the data from the database with a specific style instead of the content from the pages.
So far I managed to show some text in every specific page.
This is my code after some basic configurations:
global $wpdb;
global $wp_query;
add_action( 'wp', 'check_which_page' );
function check_which_page()
{
$page_type=get_post_type();
$page_id=get_the_ID();
//echo $page_id;
switch($page_id)
{
case 50:technologyPage();break;
case 82:medicalPage();break;
}
}
function technologyPage()
{
return print "Technology";
}
function salesPage()
{
return print "Sales";
}
function medicalPage()
{
return print "Medical";
}
I've read this post, but I couldn't solve my problem.
WordPress replace content of a page (plugin)
I already read the Wordpress documentation but I havent find anything there.
I found myself a solution, using shortcodes.
global $wpdb;
global $wp_query;
add_shortcode( 'sector_page_display', 'check_which_page' );
function check_which_page()
{
$page_type=get_post_type();
$page_id=get_the_ID();
//echo $page_id;
switch($page_id)
{
case 50:technologyPage();break;
case 82:medicalPage();break;
}
}
function technologyPage()
{
return print "Technology";
}
function medicalPage()
{
return print "Medical";
}
See that instead of add_action I changed to add_shortcode
Then on everypage I will use to show info from the database I add
[sector_page_display]
in the page, so it call my method. You can add variables in there if you want.
You'll want to run that code before WordPress has fully loaded.
Try this
global $wpdb;
global $wp_query;
add_action( 'init', 'check_which_page' );
function check_which_page()
{
$page_type=get_post_type();
$page_id=get_the_ID();
//echo $page_id;
switch($page_id)
{
case 50:technologyPage();break;
case 82:medicalPage();break;
}
}
function technologyPage()
{
return print "Technology";
}
function salesPage()
{
return print "Sales";
}
function medicalPage()
{
return print "Medical";
}
I changed the add_action to now run the code when WordPress is being initialized.

Allow comment homepage URLs without http://

I want Drupal comments to work like every other blog's comments. Who are writing their homepage URL can write "www.example.com," or "http://www.example.com," and they both work. Right now, Drupal throws an error if the URL doesn't contain "http://."
You could override the comment_validate hook in /modules/comment/comment.module and modify the validation code to insert the http:// bit into the URL if it's not already there. The changed code would look something like:
if ($edit['homepage']) {
if (!strpos($edit['homepage'], "http://")) {
$edit['homepage'] = "http://" . $edit['homepage'];
}
if (!valid_url($edit['homepage'], TRUE)) {
form_set_error('homepage', t('The URL of your homepage is not valid. Remember that it must be fully qualified, i.e. of the form <code>http://example.com/directory</code>.'));
}
}
You would also need to override the comment_form_validate hook:
function comment_form_validate($form, &$form_state) {
global $user;
if ($user->uid === 0) {
foreach (array('name', 'homepage', 'mail') as $field) {
// Set cookie for 365 days.
if (isset($form_state['values'][$field])) {
setcookie('comment_info_'. $field, $form_state['values'][$field], time() + 31536000, '/');
}
}
}
$form_state['values'] = comment_validate($form_state['values']);
}
The strpos line should be
strpos($edit['homepage'], "http://") === FALSE
https://drupal.stackexchange.com/questions/1037/allow-comment-homepage-urls-without-http

WordPress redirect after successful form validation and submit

I have a custom page template with a form in page-report.php.
I do validation on it, so I need the action to lead to the same form, but I need to redirect to a different page on successful validation.
wp_redirect() is not working, because is spitting out the header() function after the output was started already.
if($_POST['report'])
{
if($validator->ValidateForm())
{
wp_redirect('http://thankyou') // redirect
}
}
I cannot use ob_start() and ob_flush() because the header is not included in this page template.
I tried to put a function in functions.php :
add_action('get_header','redirect_to');
function redirect_to($page){
if($page)
{
wp_redirect('http://www.google.com');
}
}
But that works only if I don't have the conditional if().
If I use it, the wp_redirect() is being spat out after the output was started.
What is my best approach to do this?
Thanks.
I think you have to use the save_post hook:
do_action('save_post', 'custom_add_save');
function custom_add_save($postID){
// called after a post or page is saved
if($_POST['report']) {
if($validator->ValidateForm())
{
wp_redirect('http://thankyou') // redirect
}
}
Also you could just try using a plugin instead of your own code...Gravity Forms and Contact form 7 both work well.
}
I got it...
Since I was doing everything from inside an admin page, the header was fired up before the wp_redirect() as it was explained in the question.
So I ended up making a new function at the top:
add_action('admin_init','redirect_to');
function redirect_to()
{
if ( isset($_REQUEST['action']) && 'adduser' == $_REQUEST['action'] ) {
wp_redirect($redirect);
die();
}
}
}
That is making sure that the redirect_to() function will be fired up before the header (on admin_init). Maybe not the most elegant solution, but it works perfect.
So in the case anybody is looking for "Redirect after post" in wordpress, this is how you do it:
wp_redirect($_SERVER['REQUEST_URI']);
Try this
if( $_POST['report'] ) {
if( $validator->ValidateForm() ) {
header( 'Location: http://thankyou' ) ;
}
}

Resources