Text formatting not working with qTranslate plugin - wordpress

I am creating a multilingual website using wordpress as CMS.
i am using qTranslate plugin for multi language support. As soon as i enable the plugin, all formatting of text is lost. i.e. all paragraphs in the editor are merged into a single paragraph.
I have enabled this option in settings: "WordPress should correct invalidly nested XHTML automatically" but there is no change.
Any suggestions please?

Add this code at the end of wp-content\themes\your-theme-name\functions.php:
//************************** PATCH START *****************************
// Fix for qTranslate plugin (that does not retain formatting in pages)
if('html' != wp_default_editor()) {
remove_filter('the_editor_content', 'wp_richedit_pre');
add_filter('the_editor_content', 'wp_htmledit_pre');
}
remove_filter( 'the_content', 'wpautop' );
if ( ! function_exists('tmce_replace') ) {
function tmce_replace() {
$tadv_options = get_option('tadv_options', array());
$tadv_plugins = get_option('tadv_plugins', array());
?>
<script type="text/javascript">
if ( typeof(jQuery) != 'undefined' ) {
jQuery('body').bind('afterPreWpautop', function(e, o){
o.data = o.unfiltered
.replace(/caption\]\ +?<\/object>/g, function(a) {
return a.replace(/[\r\n]+/g, ' ');
});
}).bind('afterWpautop', function(e, o){
o.data = o.unfiltered;
});
}
</script>
<?php
}
add_action( 'after_wp_tiny_mce', 'tmce_replace' );
}
function teslina_tinymce_config( $init ) {
$init['remove_linebreaks'] = false;
$init['apply_source_formatting'] = true;
$init['preformatted'] = true;
$init['remove_redundant_brs'] = false;
return $init;
}
add_filter('tiny_mce_before_init', 'teslina_tinymce_config');
//************************** PATCH END *******************************

write simple post in html mode to test like this :
<p>lorem</p>
<p>ipsum</p>
There is filter in wordpress to format text (plugin, or functions.php) try to find them, else try another plugin like Polylang.

I found a solution here:
http://www.teslina.com/en/748/wordpress/qtranslate-code-syntax-bugfix/
However i suspect that the code added in plugin will be removed when it's updated.

Related

Override wordpress routing / url rewriting?

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
}

How can I add no-follow to all links in a WordPress site in one go?

I'm coding a WordPress website in which I want all the external links to be "no-follow" by default.
It is quite long to go and edit each single link to add the "no-follow", is there a way to code it once for all?
Thank you
IMHO, the best way to do so is using a custom filter. Being generated dinamically by PHP, you can edit links on post and pages server-side even before a user visit them: WordPress used to add rel="nofollow" to each link by default in the past, but I see it doesn’t happen anymore. Then, I found a solution by Debjit Saha you may try on functions.php.
add_filter('the_content', 'my_nofollow');
add_filter('the_excerpt', 'my_nofollow');
function my_nofollow($content) {
return preg_replace_callback('/<a[^>]+/', 'my_nofollow_callback', $content);
}
function my_nofollow_callback($matches) {
$link = $matches[0];
$site_link = get_bloginfo('url');
if (strpos($link, 'rel') === false) {
$link = preg_replace("%(href=\S(?!$site_link))%i", 'rel="nofollow" $1', $link);
} elseif (preg_match("%href=\S(?!$site_link)%i", $link)) {
$link = preg_replace('/rel=\S(?!nofollow)\S*/i', 'rel="nofollow"', $link);
}
return $link;
}
Please, notice that some plugins could break this code and I’ve never tried this on newer WordPress versions.
One way you can do it is using JavaScript:
var hrefToCheck = "mysite.com" // change this string
noFollowExternalLinks(hrefToCheck); // call to run the function below
function noFollowExternalLinks(siteHref) {
document.querySelectorAll('a').forEach(function(link) {
if (!link.href.includes(siteHref)) {
link.setAttribute("rel", "nofollow");
}
})
}
So, you can change hrefToCheck variable to a piece of your site URL that will be checked inside each link inside the page.
The function will loop to check every link in the page and apply rel="nofollow" to all external links (that don't match the text in the variable).

In wordpress to mark required fields in Add New Post page

in WP 4.2 opening Add New Post page and without editing any fields, clicking on "Publish" button page is submitted and reopened, but no any of fields are marked with red background color as required.
How to make it? Seems, that is original behauvior of this page.
I want it to work like editor of categories, when in similar situation field name is marked with red background color as required.
Also I added several fields to New Post page using register_post_type function and I aslo want to marked with red background color as required. Which is the best way for this?
Thanks!
You can call your jquery validation by your self using below code,
Add this code in theme's functions.php and custom.js in your theme js folder
add_action( 'admin_print_scripts-post-new.php', 'custom_admin_script', 11 );
add_action( 'admin_print_scripts-post.php', 'custom_admin_script', 11 );
function custom_admin_script() {
global $post_type;
// Post type = "post" or "page" you can load script wheater it is post or page , if page then change 'post' to 'page' in below condition
if( 'post' == $post_type )
wp_enqueue_script( 'custom-admin-script', get_stylesheet_directory_uri() . '/js/custom.js' );
}
custom.js
jQuery(document).ready(function ($) {
if ($("#post").length > 0) {
var frm = true;
$("#publish").click(function () {
var title = $("#title").val();
var excerpt = $("#excerpt").val();
// your custom field variable goes here
if (jQuery.trim(title) == "" || jQuery.trim(excerpt) == "") {
frm = false;
}
if (frm == false) {
$("#publish").prop('disabled', true);
} else {
$("#publish").prop('disabled', false);
}
});
}
});

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 add ads to the end of RSS on wordpress?

I have added a function to functions.php on my theme.
function insertAds($content) {
$content = $content.' add goes here';
return $content;}
add_filter('the_content_feed', 'insertAds');
add_filter('the_excerpt_rss', 'insertAds');
The problem is that I'm having the add displayed under each content, and not at the end of the rss page. How can I fix that?
WordPress doesn’t offer a hook for what you want to do. In which element would you place the ad?
The usual RSS-2-Feed has meta data and items (the content). There is no other element.
See wp-includes/feed-rss2.php for details.
Update
Adjust the following code to your needs and put the file into your plugin directory:
<?php
/*
Plugin Name: Last man adding
Description: Adds content to the last entry of your feed.
Version: 0.1
Author: Thomas Scholz
Author URI: http://toscho.de
Created: 31.03.2010
*/
if ( ! function_exists('ad_feed_content') )
{
function ad_feed_content($content)
{
static $counter = 1;
// We only need to check this once.
static $max = FALSE;
if ( ! $max )
{
$max = get_option('posts_per_rss');
}
if ( $counter < $max )
{
$counter++;
return $content;
}
return $content . <<<MY_ADDITIONAL_CONTENT
<hr />
<p>Place your ad here. Make sure, your feed is still
validating</p>
MY_ADDITIONAL_CONTENT;
}
}
add_filter('the_content_feed', 'ad_feed_content');
add_filter('the_excerpt_rss', 'ad_feed_content');
Is this the effect you had in mind? As you can see, adding content is rather easy. :)

Resources