In Wordpress Visual Composer, is it possible to set default values for the fields under Select Custom Fields? - wordpress

Specifically I'd like to give every new post a particular sidebar by default. Seems like setting default values should be possible, but I can't see where in the interface and a Google turned up nothing, so any advice appreciated. Thanks!

You can do this by using the "value" property of the parameter array(s) for vc_map() or vc_add_parameter(). Documentation is here... see the description of the value property.
An example would be:
function vc_myshortcode(){
vc_map({
'name' => 'My Shortcode',
'base' => "myshortcode",
'params' => array(
array(
'type'=>'textfield',
'holder'=>'div',
'param_name'=>'color',
'value'=>'#333333'
)
)
})}
add_action( 'vc_before_init', 'vc_myshortcode' );
So the default value for the color parameter would then be set to #333333

Related

Rank Math metabox preview for custom field

I've created a custom field in Rank Math...
add_action( 'rank_math/vars/register_extra_replacements', function(){
rank_math_register_var_replacement(
'op_shortcode',
[
'name' => esc_html__( 'OP ACF Field', 'rank-math' ),
'description' => esc_html__( 'Custom ACF field from ACF shortcodes.', 'rank-math' ),
'variable' => 'op_shortcode(field-name)',
'example' => esc_html__( 'Chrisad field value', 'rank-math' ),
],
'get_op_shortcode_callback'
);
});
function get_op_shortcode_callback( $shortcodename, $post_id ) {
global $post;
$post_id = "option"; // options page
$shortcodename = get_field( $shortcodename, $post_id, true );
return $shortcodename;
}
... it works in the frontend but I can't get it to be previewable in the backend metabox.
I wrote to Rank Math and the answer they gave first was, "There’s no additional code to be added to the filter since the custom variable is already working on the front end. For the preview to work, you need to ensure that the current content editor you are working on has WordPress editor support." (Which it does as far as I know because every other Rank Math variable is showing in the preview metabox).
Then they said, "When this happens it means that the custom variable you created contains code that cannot be rendered in the backend because the data is still not yet available." (I don't know what this means.)
And then finally, "The following article explains the default WordPress hooks firing sequence: http://rachievee.com/the-wordpress-hooks-firing-sequence/. Beyond that article, you will have to research further into how to get the variable to load. ACF support/forums might be able to help." (That's a 2015 article on hooks firing sequence which leads me to believe that there is a hook firing out of sequence but I can't figure it out).
I am just looking to see if anyone has any idea how to get the custom field I've registered to show up in the preview metabox in the backend (it is showing in the frontend).
Hope that makes sense.
Any help or insight is appreciated.

How to add/Modify Redux Framework options in Child Theme

I am using a paid theme which is using Redux Framework. I am trying to add a new field in Footer Options. I am able to add this field in options but only in print_r() function it is showing not showing in Themes Options Panel.
function add_another_section_bl($sections){
$sections[12]['fields'][] = array(
'id' => 'rd_footer_message_1',
'type' => 'textarea',
'title' => __('Text 2 to display in footer under bar', 'thefoxwp'),
'subtitle' => __('write your copyright information or anything you\'d like.', 'thefoxwp'),
'validate' => 'html', //see http://codex.wordpress.org/Function_Reference/wp_kses_post
'default' => 'Copyright 2015 Tranmautritam\'s team | All Rights Reserved'
);
return $sections;}
add_filter("redux/options/rd_data/register", 'add_another_section_bl');
In array data it is showing the required data but not in Options Panel in wordpress dashboard.
Kindly get me out of this.
There's a much simpler approach if you use the Redux API:
https://docs.redux.io/configuration/redux-api.html
You can modify, add, update any section or field before it is rendered. Please note, you may have to put your code within hooks. Also please note the Redux 4 method names have changed from setArgs to set_args (camelcase to non).
Otherwise, you will have all you need.

WordPress: wp_login_form how to add class name or placeholder text

How can I add placeholder or CSS classes to login fields using wp_login_form?
The function wp_login_form in wp-includes/general-template.php renders the login form from an array of arguments:
$default = array(
'echo' => true,
...
...
);
and then creates the <form>...</form>. There is no way I can add a class name to input fields or the submit button. I want to use bootstrap classes for this purpose. Currently I have to override default classes rendered by WordPress.
I don't want to do that. For example if I want to make username box to look like an input with class form-control, I have to either write additional classes in my CSS for default WP classes or take help of jQuery to remove default classes and add mine.
What is the best way to do it? wp_login_form does not have attributes set for placeholder.
In brief I need to pass the following:
1. Pass class name from outside via an array $args,
2. Pass placeholder text for input fields
This might be a solution, eventhough it isn't perfect.
Wordpress' functon wp_login_form() does not support changing the css class. And I would recommend not to use Javascript for changing the DOM when you do not absolutely have to.
My solution to modify the CSS class was to set the echo property to false so the function returns the result as a string. Then, use str_replace() to find the classes we want to replace and replace it with the class names we want to use.
$args = array(
'echo' => false,
// etc...
And now replacing the class names...
$output = wp_login_form( $args );
$output = str_replace( 'class="input"', 'class="df-input"', $output );
echo $output;
You could also convert $output into a DOMDocument() so you can replace the class names in an more elegant manner.
According to its documentation, wp_login_form() does not receive an argument to set a class name for itself of input elements. Presumably because you wouldn't have more than one login form on the page.
It is possible to add an id to the inputs of the forms like so:
$args = array(
'form_id' => 'loginform',
'id_username' => 'user_login',
'id_password' => 'user_pass',
'id_remember' => 'rememberme',
'id_submit' => 'wp-submit',
);
However, seems that in order to set other attributes such as class and placeholders you would have to use Javascript. This can be done without JQuery. This would look like something along the lines of document.querySelector('#username').setAttribute("class", "username");
See the querySelector documentation.

hook_nodeapi() - How to order additional fields

I used hook_nodeapi to add my custom field to a type of node
$node->content['my_new_field'] = array(
'#value' => $content,
);
However the new field is only appeared at the end of the content. Is there anyway for me to select a place for it to display ? e.g: between Title and Body.
For some reason I won't be able to use CCK, I want to do it programmatically.
Thanks in advance
There is something called weight. If you llok at the code from API docs, you'll see how that is supposed to work. Lower numbers appear before higher numbers.
So you could do something like
$node->content['my_new_field'] = array(
'#value' => $content,
'#weight' => 5, //play with the values until you are happy with the output
);
Couldn't you implement hook_load instead to append the node object with your custom fields:
http://api.drupal.org/api/function/hook_load/6
Then you could theme it however you want in node-customtype.tpl.php. Just a thought.

Drupal form with custom ID

Correct me if I'm wrong, after reading drupal fapi related articles, I got the impression that fapi generates 'id' attributes by itself. It allows developers to assign 'name' attribute only. If that's the case, is there a way I can set desire 'id' value for elements? Because, I want my elements to have meaningful 'id' so that html/jquery code would be easier to read as well as save my time from going through already written jquery code to change those all 'id's that I've used inside.
P.S:drupal version - 6.x
Ok found the solution. I can use the #attributes key of the $form element to set any additional attributes (such as class, id, etc.). Thanks for your help so far.
I had a similar issue to deal with. I needed to have multiple forms on the same page so I had to change the ids of the form and its elements to prevent duplicate ids. I did something like the following:
function voci_comment_form($form, &$form_state, $cid) {
$form['#attributes']['id'] = 'voci-comment-form-' . $cid;
$form['#attributes']['class'][] = 'voci-comment-form';
$form['body'] = array(
'#title' => 'Post a comment',
'#type' => 'textarea',
'#resizable' => FALSE,
'#rows' => 1,
);
$form['comment'] = array(
'#type' => 'submit',
'#value' => 'Comment',
);
foreach ($form as $k => &$element) {
$k = str_replace('_', '-', $k);
$element['#attributes']['id'] = "edit-$k-$cid";
$element['#attributes']['class'][] = "edit-$k";
}
return $form;
}
This basically sets unique ids based on the $cid that is passed in. The code also adds classes to each element in the form so you can style it easily. I'm sure a more robust solution is possible but this is the basic idea. Tested in Drupal 7.
It's true that you can set $element['#attributes']['id'] and that will apply to the form field. However, it will break labels and #states in Drupal 7 because the rest of the rendering pipeline reads the ID from somewhere else. So for your labels and #states to keep working, use set the ID to $element['#id'] instead (an undocumented property that nonetheless is how the form API watches ID internally).
Make sure to pass your ID through drupal_html_id as well to ensure no conflicts.
This problem doesn't really have much to do with the Drupal-FAPI itself, but more with how Drupal theme forms (create the markup).
If you want to alter all forms on your site, you can overwrite the theming functions that is used for forms and the different type of form fields.
If you just want to overwrite some forms or form fields, you can set the #theme attribute on the form or an element, to change which function should be used for creating the markup.

Resources