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
Related
I would like to serve different content based on the URL.
I started with a custom page setup via a custom template but I am open to something else.
The main goal is to have one PHP page that can serve different contents programmatically based on the URL.
For example:
https://some-url.com/my-plugin/ -> run my page
https://some-url.com/my-plugin/foo/ -> run my page
https://some-url.com/my-plugin/foo2/abc/ -> run my page
etc...
I have been looking at add_rewrite_rule, add_rewrite_tag, flush_rewrite_rules and WP_Rewrite API but I am getting confused about which one I should use?
I found an example here but I could not make it work, I get 404s, any idea why?:
/*
Plugin Name: Products Plugin
Plugin URI: http://clivern.com/
Description: Register URL rules for our products
Version: 1.0
Author: Clivern
Author URI: http://clivern.com
License: MIT
*/
function products_plugin_activate() {
products_plugin_rules();
flush_rewrite_rules();
}
function products_plugin_deactivate() {
flush_rewrite_rules();
}
function products_plugin_rules() {
add_rewrite_rule('products/?([^/]*)', 'index.php?pagename=products&product_id=$matches[1]', 'top');
}
function products_plugin_query_vars($vars) {
$vars[] = 'product_id';
return $vars;
}
function products_plugin_display() {
$products_page = get_query_var('pagename');
$product_id = get_query_var('product_id');
if ('products' == $products_page && '' == $product_id):
//show all products
exit;
elseif ('products' == $products_page && '' != $product_id):
//show product page
exit;
endif;
}
//register activation function
register_activation_hook(__FILE__, 'products_plugin_activate');
//register deactivation function
register_deactivation_hook(__FILE__, 'products_plugin_deactivate');
//add rewrite rules in case another plugin flushes rules
add_action('init', 'products_plugin_rules');
//add plugin query vars (product_id) to wordpress
add_filter('query_vars', 'products_plugin_query_vars');
//register plugin custom pages display
add_filter('template_redirect', 'products_plugin_display');
First of all, make sure your pretty permalinks are enabled, in this case the option "Plain" in Settings - Permalinks should be unselected:
Select one of these options to enable pretty permalinks
You can check whether pretty permalinks are enabled in the code like so:
function is_enabled_pretty_permalinks() {
return !empty( get_option( 'permalink_structure' ) );
}
if ( is_enabled_pretty_permalinks() ) {
echo 'Pretty permalinks enabled';
}
Next add a new rewrite rule:
function add_my_rewrite_rule() {
$page_slug = 'products'; // slug of the page you want to be shown to
$param = 'do'; // param name you want to handle on the page
add_rewrite_rule('my-plugin/?([^/]*)', 'index.php?pagename=' . $page_slug . '&' . $param . '=$matches[1]', 'top');
}
add_action('init', 'add_my_rewrite_rule');
Add your parameter to query vars so you will be able to handle it on the page:
function add_my_query_vars($vars) {
$vars[] = 'do'; // param name you want to handle on the page
return $vars;
}
add_filter('query_vars', 'add_my_query_vars');
Then you can access your query var do in the page template or in a shortcode, for example:
function my_plugin_shortcode_handler( $atts ){
$do = get_query_var( 'do' );
if ( $do === 'this' ) {
return 'do this';
} else {
return 'do that';
}
}
add_shortcode( 'myshortcode', 'my_plugin_shortcode_handler' );
Then place the shortcode to the content via Gutenberg.
Check out the links:
https://some-url.com/my-plugin/this/ - outputs "do this"
https://some-url.com/my-plugin/that/ - outputs "do that".
This can be solved by using query params. Like you mentioned you have set up custom page via a custom template. You can read the URL and check for the parameters and based on that you can send data from the PHP template page.
e.g,
function cleanTheInput($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
$data = htmlentities($data);
return $data;
}
if (isset($_GET['page_url'])) {
$theUrl = cleanTheInput($_GET['page_url']);
}
if($theUrl == 266)){
// data for https://some-url.com/?page_url=266
}
if($theUrl == 366)){
// data for https://some-url.com/?page_url=366
}
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;
}
My WordPress site has a portfolio that is at www.mysite.com/portfolio/. The portfolio sections and items are administered through a custom plugin I created. I want to access the individual items like www.mysite.com/portfolio/my-cool-photo and have that put "my-cool-photo" into a query string like ?portfolio_item=my-cool-photo so I can read it from my code.
In the plugins activation PHP file I have this code:
function add_rewrite_rules($wp_rewrite) {
$new_rules = array(
'portfolio/(.+)/?$' => 'index.php?&portfolio_item=$1'
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'add_rewrite_rules');
function query_vars($public_query_vars) {
$public_query_vars[] = "portfolio_item";
return $public_query_vars;
}
add_filter('query_vars', 'query_vars');
This adds the rewrite rule to the array OK. The problem is it's not doing anything. When I go to www.mysite.com/portfolio/testing/ I get the "This is somewhat embarrassing, isn’t it?" WordPress 404 error page. Obviously the redirect isn't working, so the query string won't be filled, but just to make sure I did this:
global $wp_query, $wp_rewrite;
if ($wp_rewrite->using_permalinks()) {
$searchKey = $wp_query->query_vars['portfolio_item'];
} else {
$searchKey = $_GET['portfolio_item'];
}
...and sure enough the query string isn't getting passed.
Is there something I'm missing?
After you update the WordPress rewrite rules, you need to flush them:
http://codex.wordpress.org/Function_Reference/flush_rewrite_rules
You can choose to flush with the $hard parameter true, and then you should be able to see your rewrite rules in the .htaccess file.
Here is the example rewriting url code. Hopefully this will help users.
In this url like http://www.domainname.com/test-page?urltags=ABCD is run as http://www.domainname.com/test-page/ABCD
<?php
function add_my_rule() {
global $wp;
$wp->add_query_var('tags');
add_rewrite_rule('test-page/([^/]*)','index.php?pagename=test-page&urltags=$matches[1]','top');
}
function add_my_tags() {
add_rewrite_tag('%urltags%', '([^&]+)');
}
add_action('init', 'add_my_rule');
add_action('init', 'add_my_tags', 10, 0);
?>
If I have a custom post type named 'my_type', how can I get Wordpress to make date-based archives, for example:
mysite.com/my_type/2010/
mysite.com/my_type/2010/07/
mysite.com/my_type/2010/07/28/
I'm looking for tips both on creating the rewrite rules and on linking the urls to templates.
Thanks!
Update:
I've tried the following in my functions.php (EVENT_TYPE_SLUG is a constant defined elsewhere):
function event_rewrite_rules() {
global $wp_rewrite;
$new_rules = array(
EVENT_TYPE_SLUG."/([0-9]{4})/([0-9]{1,2})/$" => 'index.php?post_type=event&year='.$wp_rewrite->preg_index(1).'&monthnum='.$wp_rewrite->preg_index(2),
EVENT_TYPE_SLUG."/([0-9]{4})/$" => 'index.php?post_type=event&year='.$wp_rewrite->preg_index(1),
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'event_rewrite_rules');
the rewrite rules show up in $wp_rewrite-> rules, but when I navigate to those pages I get a 404 error. If I manually navigate to mysite.com/index.php?post_type=event&year=2010, I get redirected to mysite.com/2010?post_type=event
Benj I think WordPress automatically creates archives for custom post type
OK ... took some time but I figured this out (there may be more elegant solutions out there).
1) Create a standard wordpress page to serve as the archive page (and to give me access to the template). I defined the page's slug as a constant (EVENT_ARCHIVE_BASE) so that it's just hard-coded in one place and referenced everywhere else.
2) Make custom rewrite rules that catch that page's urls and redirect them to the page:
function event_date_queryvars($qvars) {
array_push($qvars, 'eyear', 'emonth');
return $qvars;
}
add_filter('query_vars', 'event_date_queryvars');
function event_date_rewrite_rules() {
// Adds rewrite rules for date-based event archives
global $wp_rewrite;
$new_rules = array(
EVENT_ARCHIVE_BASE."/([0-9]{4})/([0-9]{1,2})/?$" =>
"index.php?pagename=".EVENT_ARCHIVE_BASE."&eyear=".$wp_rewrite->preg_index(1)."&emonth=".$wp_rewrite->preg_index(2),
EVENT_ARCHIVE_BASE."/([0-9]{4})/?$" => "index.php?pagename=".EVENT_ARCHIVE_BASE."&eyear=".$wp_rewrite->preg_index(1),
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'event_date_rewrite_rules');
3) At the top of my page template, I check for the custom query vars and build the page accordingly. (I should have been able to use the built-in year and monthnum, but couldn't get them to work properly. Future troubleshooting):
// Was the page called with a specific year, month, or just plain?
$year = get_query_var('eyear');
$monthnum = sprintf('%02d', get_query_var('emonth'));
if ($monthnum) {
$list_type = 'Month';
$monthname = $month[$monthnum];
$heading = "Events from $monthname, $year";
} elseif ($year) {
$list_type = 'Year';
$heading = "Events from $year";
} else {
$list_type = 'AllPast';
$heading = get_the_title();
}
Thanks for the help, and hope this is useful for someone else! Let me know if you have a simpler/built-in way to do this.
I managed to find a more elegant/built-in solution.
The trick is in the "post_type=article" parameter.
If you create a rewrite like so:
'(articles)/([0-9]{4})' => 'index.php?post_type=article&eyear=' . $wp_rewrite->preg_index(2)
This will then keep your URL structure, but go through the default 'archive' template.
Then you steal the template away from the default to your own archive template. (for instance 'archive-articles.php')
This function accomplishes this (works for me):
add_action("template_redirect", 'my_template_redirect');
// Template selection
function my_template_redirect()
{
global $wp;
global $wp_query;
if ($wp->query_vars["post_type"] == "article")
{
if (have_posts())
{
include(TEMPLATEPATH . '/archive-articles.php');
die();
}
else
{
$wp_query->is_404 = true;
}
}
}
You'll still need to create your own methods to handle the get_query_var()'s for the year/month/day, but at least you don't need to create a custom page template.
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.