Context:
I am developing a custom Wordpress theme.
Objective:
On the home page, when you click, "stamps" are dotted about the window. I have the js functioning. Here's an example in which you can see exactly what I'm trying to do: https://omg-draft.superhi.com/
Problem:
I need a way to access the veggies in my main.js. I'm having trouble passing them from my functions.php to main.js. I have enqueued the js file and passed in the image files. In the console, I get back 'veggies is not defined'.
Here's the code from functions.php
add_action( 'admin_init', 'omedte_get_scripts' );
function omedte_get_scripts() {
wp_enqueue_script('main.js', get_template_directory_uri() . "/main.js", array(), null, true);
$veggies = array(
"beetroot" => get_theme_file_uri('/assets/beetroot.svg'),
"broccoli" => get_theme_file_uri('/assets/broccoli.svg'),
"capsicum" => get_theme_file_uri('/assets/capsicum.svg'),
"carrot" => get_theme_file_uri('/assets/carrot.svg'),
"corn" => get_theme_file_uri('/assets/corn.svg'),
"gourd" => get_theme_file_uri('/assets/gourd.svg'),
"kale" => get_theme_file_uri('/assets/kale.svg'),
"pumpkin" => get_theme_file_uri('/assets/pumpkin.svg'),
"spinach" => get_theme_file_uri('/assets/spinach.svg')
);
wp_localize_script( 'omedte_javascript_file', 'veggies', $veggies );
}
and here's my main.js:
let carrot = veggies.carrot;
let broccoli = veggies.broccoli;
let capsicum = veggies.capsicum;
let stamps = [
carrot,
broccoli,
capsicum
]
Might not be the only issue, but your handles don't match in your enqueue and localise statements.
Refer wp_localize_script()
Related
I'm adding some custom code to dynamically display Social Network icons in a Wordpress theme, by using the Customize API.
The only problem that prevents it to work as expected is that I need to find a way to access an array variable from another file, not only from the one where it is declared.
First, I've added some code to functions.php, to create some text fields in the Theme Customizer, as follows:
function my_customizer_options($wp_customize) {
// Section
$wp_customize -> add_section ( 'my_social', array(
'title' => __('Social', 'my-theme'),
'description' => __('Social Network profiles', 'my-theme'),
'priority' => 20,
)
);
// Social Networks list
$socialnetworks = array(
// Social Network => Icon Name
'facebook' => 'fa-facebook-f',
'linkedin' => 'fa-linkedin-in',
'instagram' => 'fa-instagram',
'twitter' => 'fa-twitter',
);
// Settings and controls
foreach ($socialnetworks as $key => $value) {
$wp_customize -> add_setting ( $key, array( 'default' => '' ) );
$wp_customize -> add_control ( $key, array(
'type' => 'url',
'label' => __(ucfirst($key), 'my-theme'),
'section' => 'my_social',
));
}
add_action( 'customize_register', 'my_customizer_options' );
This lets me add items to the $socialnetworks array at will, and automatically generates the code required to add settings and controls, without having to do it manually by writing down the same bits of code again and again. I.e., if I need to add more fields for more social networks, I can simply add entries to the $socialnetworks array without duplicating the whole // Settings and controls code.
Until now, it works perfectly, I now have four text fields in the customizer where I can insert URLs of social network profiles. Now I need to access these values from inside the theme template files.
This is the portion of my header.php file where I want to display the icons that link to the respective Social Network profiles:
/*
// Social Networks list
$socialnetworks = array(
// Social Network => Icon Name
'facebook' => 'fa-facebook-f',
'linkedin' => 'fa-linkedin-in',
'instagram' => 'fa-instagram',
'twitter' => 'fa-twitter',
);
*/
// Social Icon
foreach ($my_socialnetworks as $key => $value) {
if ( get_theme_mod($key, '')) { ?>
<i class="fab <?php echo $value ?>"></i>
<?php };
}
This is supposed to retrieve the values inserted in the Customizer with the get_theme_mod() WP builtin functions, and dinamically add the icons and relative links for each inserted URL.
The problem is that in order to make it work, I need to uncomment the $socialnetworks array, and basically redefine it also from inside header.php, while I want instead to access it from functions.php.
A possible problem could be that $socialnetworks is defined inside the my_customizer_options() function; I tried to move it outside, before the function itself, but in this way it even breaks the code in functions.php without solving anything.
I need the correct place and the correct way to define $socialnetworks once and being able to access it from any file of my theme, how should I modify my code in order to achieve this?
As suggested by an user in the comments, I put the array in a function, now I can access the array from anywhere by calling the function.
Here is the modified code:
functions.php:
// Social Networks list
function my_socialnetworks() {
$socialnetworks = array(
// Social Network => Icon Name
'facebook' => 'fa-facebook-f',
'linkedin' => 'fa-linkedin-in',
'instagram' => 'fa-instagram',
'twitter' => 'fa-twitter',
);
return $my_socialnetworks;
}
function my_customizer_options($wp_customize) {
// Section
$wp_customize -> add_section ( 'my_social', array(
'title' => __('Social', 'my-theme'),
'description' => __('Social Network profiles', 'my-theme'),
'priority' => 20,
)
);
$my_socialnetworks = my_socialnetworks();
// Settings and controls
foreach ($socialnetworks as $key => $value) {
$wp_customize -> add_setting ( $key, array( 'default' => '' ) );
$wp_customize -> add_control ( $key, array(
'type' => 'url',
'label' => __(ucfirst($key), 'my-theme'),
'section' => 'my_social',
));
}
add_action( 'customize_register', 'my_customizer_options' );
and header.php:
$my_socialnetworks = my_socialnetworks();
// Social Icon
foreach ($my_socialnetworks as $key => $value) {
if ( get_theme_mod($key, '')) { ?>
<i class="fab <?php echo $value ?>"></i>
<?php };
}
is it somehow possible to update a wordpress-plugin via another plugin with php code?
i tried something like this
$request = wp_remote_post(
'http://wordpress2/wp-admin/admin-ajax.php',
array(
'body' => array(
'plugin' => 'hello-dolly/hello.php',
'slug' => 'hello-dolly',
'action' => 'update-plugin',
'_ajax_nonce' => wp_create_nonce( 'nonce-test' ),
)
));
but this only leads in a 400 status...
I thought this kind of stuff would be easy in wordpress, dumb me! :-D
Found a solution:
after all plugins are loaded add a filter which define to auto-update plugins
add_action( 'plugins_loaded', array( CLASS, 'abpr_plugins_loaded' ), 1 );
public static function abpr_plugins_loaded(){
add_filter( 'auto_update_plugin', '__return_true');
}
the define some function which will be triggered from something, in my case it is an custom api-url which calls this:
public static function update_all_defined_plugins($data){
set_site_transient( 'update_plugins', '' );
wp_maybe_auto_update(); }
keep in mind that this is some bare stuff here without any code- & results-checking
Goodmorning everyone,
I'm new in Drupal.
I hope don't ask a stupid question.
I'm working with Drupal 7 and I need to edit a custom module for my company developed by another developer.
This is a piece of code where I use "theme" function.
This code is under "sites/all/modules/gestione_attivita_attivita/gestione_attivita_attivita.module"
function gestione_attivita_attivita_block_search_attivita($tipo_ricerca) {
$block['subject'] = "";
$ricerca = gestione_attivita_ricerca_fetchAll($tipo_ricerca);
$block['content'] = theme('ricerca_attivita', array(
'items' => $ricerca,
'tipo_ricerca' => $tipo_ricerca
));
return $block;
}
I know that should exist "ricerca_attivita" hook declared somehere in my files.
I'v been looking for something like "['ricerca_attivita'] = array(" or similar words or sub-words in all my files of my site folder but it doesn't exist.
The only thing I know is that under :"sites/all/themes/customModuleOfmyCompany/templates" there are several tpl files and in particular one called "ricerca_attivita.tpl.php" that work and receives data from theme function but I don't know how this is possible.
I don't know who tell to theme call to go on another folder on another path and use "ricerca_attivita.tpl.php" and not foo.tpl.php for example.
Is there anyone that can help me?
Another thing:
Going under includes/theme.inc and debugginng it I have this printing hook info:
array (
'template' => 'ricerca_attivita',
'path' => 'sites/all/themes/customtheme/templates',
'type' => 'theme_engine',
'theme path' => 'sites/all/themes/customtheme',
'preprocess functions' =>
array (
0 => 'template_preprocess',
1 => 'contextual_preprocess',
),
'process functions' =>
array (
0 => 'template_process',
1 => 'ctools_process',
2 => 'rdf_process',
),
)
but I don't know who is that declare it
I think you should use the developer module for themers https://www.drupal.org/project/devel_themer
And about theming function, did you search for this function inside the themes folder?
Hope that helps.
I've been working on this for a while and even though i've searched through a lot of stackoverflow questions/answers, i haven't been able to find what i'm looking for.
My question:
I'm working on developing my first genesis wordpress child theme, particularly now the customizer. I want the user to be able to have different options, one of which is choosing from a few background patterns that I've designed for a certain div class. There are three different patterns, and I've been able to so far make three radio buttons. Here is the code:
$wp_customize->add_setting('BG_Pattern', array( 'default' => '#f5ebdf',));
$wp_customize->add_control('BG_Pattern', array(
'label' => __('Background Pattern', 'FoxiePro'),
'section' => 'backgrounds',
'settings' => 'BG_Pattern',
'type' => 'radio',
'choices' => array(
'tan.jpg' => 'Tan',
'#e6e6e6' => 'gray',
'teal' => 'teal',
),
));
And the output is this, in the header.php file:
<?php
$BG_Pattern = get_theme_mod('BG_Pattern');
?>
<style>
.enews-widget {background-image: url( '<?php echo $BG_Pattern; ?>' );}
</style>
Where it says "tan.jpg", is where I would like to put in a url to tan.jpg src, which is in my child theme folder. However, any link I put in doesn't make the pattern appear. Inputting something like:
'bloginfo('template_url'); ?>/images/tan.jpg' => 'Tan',
also hasn't worked for me. Anyone have any ideas? Thanks!
Figured it out, because it's a child theme, needed to use the following:
get_stylesheet_directory_uri() . '/images/backgrounds/tan.jpg' => 'Tan',
I'am working on a sorter plugin for WordPress, which have Redux Framework installed to manage the options of every section. The plugin uses AJAX to get the ids of all the sections in the homepage of the website, then passes those values to the plugin main file to process in a function that stores the values in the current user meta. That works well, no problem here. The function looks like this:
add_action( 'wp_ajax_save_sections_ids', 'save_sections_ids_sorting_sections' );
function save_sections_ids_sorting_sections() {
//stuff here...
$user_ide = get_current_user_id(); //it works because it is inside a hook
update_user_meta($user_ide, "set-sections", $sections_ids);
die();
}
Then I have to get the stored values in user_meta to pass them to the Redux Field, so I wrote other function in the main file of the plugin. The function is this:
function get_the_db_sections_ids() {
$user_ide = 1; //This should be get_current_user_id() or something similar, but nothing works, not even globalizing $current_user and using get_currentuserinfo();
$sections_ids = get_user_meta($user_ide, "set-sections", true);
$sorter_field = array(
"section_id" => "basic",
'id' => 'homepage-sections',
'type' => 'sorter',
'title' => 'Control de secciones',
'desc' => 'Arrastra y suelta los bloques con los nombres de las secciones, tanto para ordenarlas verticalmente como para desactivarlas o activarlas.',
'compiler' => 'true',
'options' => array(
'enabled' => array(
),
'disabled' => $sections_ids
),
);
return $sorter_field;
}
As you notice in the comment in the function above, I have tried several ways, also require_once("/../../../wp-load.php"), but nothing happens. I tried do_action and add_actions, to create a hook, but those also use global variables, and for what I understand, the global variables do not work in functions with no hooks in plugins.
But I havent finished yet. The really tricky part is, I am calling an instance of Redux class inside the Redux config file (sample-config.php for the demo, I have a custom file, but it is the same).
The instance is Redux::setField($opt_name, get_the_db_sections_ids());
The problem this does not print anything if I call it from a function, or the function linked to the AJAX call.
As you can see, the second parameter of the instance is the function I wrote above that, and it works perfectly if I set $user_ide to 1, but I want the data stores in all admins user_meta, in case user 1 is erased, or whatever.
Is there a way to achieve what I want, or to store the data somewhere else and get it from there. I was thinking in creating a custom table in db, and use wpdb to retrieve the data, but I think I can't use wpdb either, because it will be the same problem.
I get the feeling I'm missing something very basic, but I can't get it. Please help.
This should help you out
global $current_user;
get_currentuserinfo();
$user_ide = $current_user->ID;
You'll have to declare a global variable $current_user, in order to use it.
Finally I found the solution to my problem. As I said, I couldn't get the current user id from the pligin if I tried outside an action hook or a filter hook, setting sessions and global variables didn't work, and functions hooked with WP normal hooks wouldn't set the Redux field. I reviewed the sample-config.php file inside the ReduxFramework/sample folder, and I found out some functions and hooks. One of them to set the entire section, but inside that section, you can set the field and it worked.
I eresed the Redux::setField instance, and did this in the sorter plugin main file:
add_filter('redux/options/' . $opt_name . '/sections', 'dynamic_section');
if ( ! function_exists( 'dynamic_section' ) ) {
function dynamic_section( $sections ) {
global $current_user;
get_currentuserinfo();
$sections[] = array(
'title' => __( 'Section via hook', 'redux-framework-demo' ),
'desc' => __( '<p class="description">This is a section created by adding a filter to the sections array. Can be used by child themes to add/remove sections from the options.</p>', 'redux-framework-demo' ),
'icon' => 'el el-paper-clip',
'fields' => array(get_the_db_sections_ids($current_user->ID))
);
return $sections;
}
} //I copied it from sample-config.php and added what's inside `"fields" =>` and the global.
Then I modified my array function like this:
function get_the_db_sections_ids($user_ide) { //Added the parameter
//Erase $user_ide = 1;
$sections_ids = get_user_meta($user_ide, "set-sections", true);
$sorter_field = array(
"section_id" => "basic",
'id' => 'homepage-sections',
'type' => 'sorter',
'title' => 'Control de secciones',
'desc' => 'Arrastra y suelta los bloques con los nombres de las secciones, tanto para ordenarlas verticalmente como para desactivarlas o activarlas.',
'compiler' => 'true',
'options' => array(
'enabled' => array(
),
'disabled' => $sections_ids
),
);
return $sorter_field;
}
And that's it, I hope this will help somebody. Redux is great but their documentation is very far from good, so, here's one little contribution for those using the framework. And thanks to the users who tried to help me.