WordPress: Changing comment_form(); $defaults in plug-in - php

I'm using the extend comment plugin to change the comment_form(); drastically. Now I want to control the values of the $default options in the plugin. Tested the PHP below, but got the following error:
php:
add_filter( 'comment_form_defaults', $defaults );
function custom_defaults($defaults) {
$comment_field_hide = '';
$defaults[ 'label_submit' ] = 'fooBar';
return $defaults;
}
EDIT: Changed according to the comment below, error is gone, but function doesn't take effect.

Indeed, the method is "add_filter"... that's the error. Remove the "s"
it seems you do not use parameters the right way too... Second parameter should be the function's name, so change it to :
add_filter( 'comment_form_defaults', 'custom_defaults' );
function custom_defaults($defaults) {
$comment_field_hide = '';
$defaults[ 'label_submit' ] = 'fooBar';
return $defaults;
}

Related

Changing Title Tags depending on URL parameter

I have a bunch of pages, which are paginated using the url parameter ?page=
I am using wordpress and would like each page to have a custom made title. So far I've created a custom template an set up the following code:
$titletags = array('Title1','Title2');
if ($page==2) {$arrayno = 1}
function ChangeTitle($title) {
$title = $titletags[$arrayno];
return $title;}
add_filter( 'wpseo_title', 'ChangeTitle' );
I run this before the call to get the header. Now this code is not working, although it should change the title to Title2, when the url parameter is 2.
The code works if don't use the array inside the function, i.e. I do:
function ChangeTitle($title) {
$title = 'SomeTitle';
return $title;}
add_filter( 'wpseo_title', 'ChangeTitle' );
You have not access to $titletags array. first use global then get it. try this code:
$titletags = array('Title1', 'Title2');
function ChangeTitle( $title ) {
global $titletags, $page;
if ( $page == 2 ) {
return $titletags[1];
}
return $title;
}
add_filter( 'wpseo_title', 'ChangeTitle' );

Remove filter of a plugin

I have a review plugin that overrides the comment form in a specific posttype.
Now I'm trying to seperate the reviews and comments.
My first step is to remove the filter that modifies the current comment template and use that filter inside a second comment form.
The plugin uses this code (simplified)
final class DM_Reviews {
public function hooks() {
do_action_ref_array( 'dm_reviews_before_setup_actions', array( &$this ) );
add_filter( 'comment_form_defaults', array( $this, 'reviews_form' ) );
do_action_ref_array( 'dm_reviews_after_setup_actions', array( &$this ) );
}
public function review_form( $args ) {
$form = 'plugin code to modify form';
return wp_parse_args( $form, $args );
}
}
In my child theme's function.php file, I tried to use this but it didn't worked.
global $DM_Reviews;
remove_filter( 'comment_form_defaults', array($DM_Reviews, 'reviews_form'),1 );
WP Codex
If someone can put me in the right direction on how to solve it, it would help me a lot.
I think you can achieve this goal, using one of the following solutions depending on the way this plugin instantiates the class:
if( class_exists('DM_Reviews' ) ){
//This should work in whatever case, not tested
remove_filter('comment_form_defaults', array( 'DM_Reviews', 'reviews_form'));
//or Instantiating a new instance, not tested
remove_filter('comment_form_defaults', array( new DM_Reviews(), 'reviews_form'));
//or Targeting the specific instance, not tested
remove_filter('comment_form_defaults', array( DM_Reviews::get_instance(), 'reviews_form'));
}
Hope it helps, let me know if you get stuck.
for me remove_filter didn't work from function.php i wanted to remove a specific behavior of a plugin so what i did :
add_action( 'init', 'remove_filters' );
function remove_filters(){
global $wp_filter;
unset( $wp_filter["_filter_name"]);
}
Try this :
$instance = DM_Reviews::this();
remove_filter('comment_form_defaults', array( $instance, 'reviews_form'));

Custom query var in wordpress

I'm trying to give a pretty url to my custom CPT (Orders). This is the way im trying:
add_filter('post_type_link', 'custom_post_type_link', 1, 3);
function custom_post_type_link( $link, $post = 0 ){
if ( $post->post_type == 'orders' ){
$order_code = 1121000+$post->ID;
return home_url( 'orders/' . $order_code);
} else {
return $link;
}
}
Assume that post ID is 32, This code creates the following url:
http://www.example.com/orders/1121032
Now i wanna write a code to display the post with post ID=32 in above url.
I heard somewhere that i should use custom query vars. But i don't know how to use it.
This is the rest of code i've written
add_filter('query_vars', 'custom_add_query_vars');
function custom_add_query_vars($qVars){
$qVars[] = "code";
return $qVars;
}
add_action( 'init', 'custom_rewrites_init' );
function custom_rewrites_init(){
add_rewrite_rule(
'orders/([0-9]+)?$',
'index.php?post_type=orders&code=$matches[1]',
'top' );
}
After adding custom rewrite rules, wordpress needs to be told to activate the new rule.
Use this method after the add_rewrite_rule function call.
$wp_rewrite->flush_rules();
Warning: flush_rules is expensive, you definitely don't want to call it on every request. Typically you would put the custom_rewrites_init and flush_rules in a plugin register_activation_hook function.
If you're lazy, you can just add it to your code once, make a request to the website (which will rewrite the .htaccess rewrite rules), then comment the flush_rules method out.

Remove Wordpress Body Classes

How could I remove classes from the body element in Wordpress?
Default:
body class="page page-id-7 page-template-default logged-in"
What I'm looking for:
body class="page-id-7"
On Post's:
body class="postid-40"
This is how you find something like this out:
Find the relevant function by looking in a theme file, in this case header.php
Look this function up in the Wordpress Codex (http://codex.wordpress.org/Function_Reference/body_class)
Is there any examples that look similar to what I want to do? Yes:
// Add specific CSS class by filter
add_filter('body_class','my_class_names');
function my_class_names($classes) {
// add 'class-name' to the $classes array
$classes[] = 'class-name';
// return the $classes array
return $classes;
}
So basically, to remove all classes, just add this to functions.php:
add_filter('body_class','my_class_names');
function my_class_names($classes) {
return array();
}
t
4. But I want to keep page id and post id - how can I do that? Because you don't know on what index in the array that you can find this information on, and searching through the array is lame, you need to first empty the array like we did above, and then add the stuff you really want. How do you get the right information, that is, page id and post id? This is step five.
(5) At the bottom of the codex page you will find a link to the source code. This is the link you'll find: http://core.trac.wordpress.org/browser/tags/3.2.1/wp-includes/post-template.php If you look at the function, you'll see it uses a function which simply populates the same array that we can manipulate with aforementioned filter. By looking how they do it, you can do it too.
add_filter('body_class','my_class_names');
function my_class_names($classes) {
global $wp_query;
$arr = array();
if(is_page()) {
$page_id = $wp_query->get_queried_object_id();
$arr[] = 'page-id-' . $page_id;
}
if(is_single()) {
$post_id = $wp_query->get_queried_object_id();
$arr[] = 'postid-' . $post_id;
}
return $arr;
}
Hopefully this code works. If it does not, try to figure out what's wrong by following the steps above. I hope I helped at least a little bit :)
currently working code to remove and add a class
//remove body class
add_filter('body_class', function (array $classes) {
if (in_array('class_name', $classes)) {
unset( $classes[array_search('class_name', $classes)] );
}
return $classes;
});
// Add specific CSS class by body.
add_filter( 'body_class', function( $classes ) {
return array_merge( $classes, array( 'class-name' ) );
} );

Remove main editor from wordpress edit page screen

Anyone know of a way to remove the main editor from the page edit screen? And not just with css. I've added a few other meta boxes with the tinymce and they collide with the main one.
I have a class that removes other meta boxes from the edit screen, but I cant get rid of the main editor this way. I've tried to add 'divpostrich' and 'divpost' to the array in the class (but with no luck):
class removeMetas{
public function __construct(){
add_action('do_meta_boxes', array($this, 'removeMetaBoxes'), 10, 3);
}
public function removeMetaBoxes($type, $context, $post){
/**
* usages
* remove_meta_box($id, $page, $context)
* add_meta_box($id, $title, $callback, $page, $context = 'advanced', $priority = 'default')
*/
$boxes = array( 'slugdiv', 'postexcerpt', 'passworddiv', 'categorydiv',
'tagsdiv', 'trackbacksdiv', 'commentstatusdiv', 'commentsdiv',
'authordiv', 'postcustom');
foreach ($boxes as $box){
foreach (array('link', 'post', 'page') as $page){
foreach (array('normal', 'advanced', 'side') as $context){
remove_meta_box($box, $type, $context);
}
}
}
}
}
$removeMetas = new removeMetas();
I have also tried removing the 'divpostrich' with jquery. But cant figure out where to put the js for it to work. When I remove the 'postdivrich' in the browser with firebug - my remaining tinymce fields work perfect.
Any ideas?
There is built in WP support for this so you don't have to fiddle directly with the globals and ensure forwards compatibility if they ever change how features are handled. The WP Core code does pretty much the exact same logic as #user622018 answer however
function remove_editor() {
remove_post_type_support('page', 'editor');
}
add_action('admin_init', 'remove_editor');
What you are looking for is the global $_wp_post_type_features array.
Below is a quick example of how it can be used
function reset_editor()
{
global $_wp_post_type_features;
$post_type="page";
$feature = "editor";
if ( !isset($_wp_post_type_features[$post_type]) )
{
}
elseif ( isset($_wp_post_type_features[$post_type][$feature]) )
unset($_wp_post_type_features[$post_type][$feature]);
}
add_action("init","reset_editor");
Add the following code to your functions.
function remove_editor_init() {
if ( is_admin() ) {
$post_id = 0;
if(isset($_GET['post'])) $post_id = $_GET['post'];
$template_file = get_post_meta($post_id, '_wp_page_template', TRUE);
if ($template_file == 'page-home.php') {
remove_post_type_support('page', 'editor');
}
}
}
add_action( 'init', 'remove_editor_init' );
Couldn't you just disable the TinyMCE editor, leaving the HTML editor, as your meta boxes are colliding with it? :)
To disable the editor you will need to edit your wp-config.php file and add this line to the top:
define('DISALLOW_FILE_EDIT', true);

Categories