I am trying to take a custom color that was chosen by the user in the Customizer, and add it as an html attribute to the links that are outputted by a specific menu.
Here is my code so far:
// Gets the footer link color, if assigned by user.
if (get_theme_mod('theme_footer_link_color')) {
$footerLinkColorAttribute = 'color:' . get_theme_mod('theme_footer_link_color', 'default_value') . ';';
}
function add_customizer_link_color_attribute_to_footer_menu_links($atts, $item, $args){
if ($args->theme_location == 'footerNavLocation') {
$atts['style'] = $footerLinkColorAttribute;
}
return $atts;
}
add_filter('nav_menu_link_attributes', 'add_customizer_link_color_attribute_to_footer_menu_links', 10, 3);
My problem is that the style attribute is taking $footerLinkColorAttribute as plain text, instead of a variable that contains the text set by the user.
How can I get it to be taken as a variable?
I figured it out. I was defining the variable outside of the function, but needed to define it inside of the function:
function add_customizer_link_color_attribute_to_footer_menu_links($atts, $item, $args){
if ($args->theme_location == 'footerNavLocation') {
// If there is a footer link color assigned in the Customizer, this gets it ready to be inserted as an attribute into the link menu link text.
if (get_theme_mod('lets_get_started_footer_link_color')) {
$footerLinkColorAttribute = 'color:' . get_theme_mod('lets_get_started_footer_link_color', 'default_value') . ';';
}
$atts['style'] = $footerLinkColorAttribute;
}
return $atts;
}
add_filter('nav_menu_link_attributes', 'add_customizer_link_color_attribute_to_footer_menu_links', 10, 3);
Related
I'm creating a dynamic function in WordPress that requires I create my function names dynamically.
In this instance, I need to generate a unique function name for this gravityforms code:
add_filter( 'gform_validation_message', 'change_message', 10, 2 );
function change_message( $message, $form ) {
return "<div class='validation_error'>Failed Validation - " . $form['title'] . '</div>';
}
So, I have a variable (which will change from time to time) that I want to use for my function name...
$newitem = 'new_item';
...and tried this...
add_filter( 'gform_validation_message', $newitem, 10, 2 );
function $newitem( $message, $form ) {
return "<div class='validation_error'>Failed Validation - " . $form['title'] . '</div>';
}
...but obviously that does not work. But I think you get the idea of what I am trying to achieve.
Is anything like this even possible?
Any help is appreciated.
$thing = 'some_function';
$$thing = function() {
echo 'hi';
};
$some_function();
Source - Use a variable to define a PHP function
You can simply pass Anonymous function to add_filter function as parameter:
$newitem = function($message, $form) {
return "<div class='validation_error'>Failed Validation - " . $form['title'] . '</div>';
};
add_filter('gform_validation_message', $newitem, 10, 2);
I am trying to change the title tag adding a variable (product-brand), but the variable does not show up.
add_filter('wpseo_title', 'filter_product_wpseo_title');
function filter_product_wpseo_title() {
return 'hello'. $_GET[$mysite_slugs['product-brand']] .'hello';
};
}
Also tried this but no luck
add_filter('wpseo_title', 'filter_product_wpseo_title');
function filter_product_wpseo_title() {
$productbrand = $_GET[$mysite_slugs['product-brand']];
return 'hello'. $productbrand .'hello';
};
}
I'm assuming your URL has the parameter .com/?product-brand=foo. Make sure you have added the priority to your filter. This should do the trick!
add_filter('wpseo_title', 'filter_product_wpseo_title', 10, 1);
function filter_product_wpseo_title($title) {
$title = 'hello '. $_GET['product-brand'] .' hello';
return $title;
}
I'm trying to develop a plugin in Moodle. One of the requirements is to add an element to the Settings Menu, in which I was able to achieve with the help of this guide
https://docs.moodle.org/dev/Local_plugins#Adding_an_element_to_the_settings_menu
And this is my code in local/myplugin/lib.php
<?php
function local_myplugin_extends_settings_navigation($settingsnav, $context) {
// question_extend_settings_navigation
global $CFG, $PAGE;
// Only add this settings item on non-site course pages.
if (!$PAGE->course or $PAGE->course->id == 1) {
return;
}
// Only let users with the appropriate capability see this settings item.
/*if (!has_capability('moodle/backup:backupcourse', context_course::instance($PAGE->course->id))) {
return;
}*/
if ($settingnode = $settingsnav->find('courseadmin', navigation_node::TYPE_COURSE)) {
$strfoo = get_string('classrecord', 'local_myplugin');
$url = new moodle_url('/course/classrecord.php', array('id' => $PAGE->course->id));
$foonode = navigation_node::create(
$strfoo,
$url,
navigation_node::NODETYPE_LEAF,
'myplugin',
'myplugin',
new pix_icon('i/grades', $strfoo)
);
if ($PAGE->url->compare($url, URL_MATCH_BASE)) {
$foonode->make_active();
}
$settingnode->add_node($foonode);
}
}
?>
I allowed the students to see the element "Class Record" in the settings menu
My concern is that how can I hide/show Class Record I added?
Any ideas would be great!
If you want only certain users to see the link, then create an appropriate capability in local/myplugin/db/access.php, e.g. 'local/myplugin:viewclassrecord', defaulting to being assigned to the 'student' role. Then check for it in the function you have defined.
e.g.
if (!has_capability('local/myplugin:viewclassrecord', $context)) {
return;
}
In my custom template tpl file, i am showing two sections (div content) of data, one from each function of my custom module.
The data also has pagination for it, displayed below.
Initially only one block of data will be displayed. Other block of data will be hidden on the page, unless the user clicks on a tab in the page.
Now my question is, how can i show individual paginations for each of my blocks in the same page ?
If i click on a page link in block 1 and go to its page, the pagination links for another block must not be affected.
How to achieve this in Drupal 7?
// my custom module code below
// function one
func one() {
$page = strtolower($page);
$query = db_select('keyword', 'k');
$query->groupBy('k.res');
$stories = $query->extend('PagerDefault')->limit(10)->execute();
return $stories;
}
// function two
func two() {
$page = strtolower($page);
$query = db_select('videos', 'v');
$query->groupBy('v.res');
$videos= $query->extend('PagerDefault')->limit(10)->execute();
return $videos;
}
// in my custom template page
// below is first section in same page
$block_content = fun one(arg(1));
$args = array( 'parameters' => array('pg' => 'one'));
print (theme ('pager',$args));
foreach($block_content as $content)
{
// doing something to display
}
print (theme ('pager',$args));
// below is second section in same page
$block_content = fun two(arg(1));
$args = array( 'parameters' => array('pg' => 'two'));
print (theme ('pager',$args));
foreach($block_content as $content)
{
// doing something to display
}
print (theme ('pager',$args));
You can use the PagerDefault::element() method to set a unique element ID for the pager:
// First one
$stories = $query->extend('PagerDefault')->element(0)->limit(10)->execute();
// Second one
$videos = $query->extend('PagerDefault')->element(1)->limit(10)->execute();
Then use the respective element ID in the theme call:
// First one
$args = array('element' => 0, ...);
print theme('pager', $args);
// Second one
$args = array('element' => 1, ...);
print theme('pager', $args);
For each member page you have a selection of components (e.g. profile, settings, activity...)
What I am trying to do is add a new component called jobs to each page. I understand how to add the link to the navigation bar. It is just creating the page that is confusing me.
Do I add a new directory, if so how do I let buddypress know.
Having a url structure like:
example.com/member/username/jobs
is important.
Thanks!
The global variable $bp is where you want to insert the new Jobs component. By dumping the global variable $bp, you can see all of the elements contained in it, including all of the components. To easily dump $bp, add the following to the top of member-header.php:
global $bp;
foreach ( (array)$bp as $key => $value ) {
echo '<pre>';
echo '<strong>' . $key . ': </strong><br />';
print_r( $value );
echo '</pre>';
}
The array within $bp that you want to add the component 'Jobs' to is bp_nav.
In functions.php add the following:
add_action( 'bp_setup_nav', 'add_subnav_items', 100 ); //Priority must be higher than 10
function add_subnav_items() {
global $bp;
//Jobs tab
$tab_array['name'] = 'Jobs';
$tab_array['link'] = $bp->displayed_user->domain.'jobs';
$tab_array['slug'] = 'jobs';
$tab_array['parent_url'] = $bp->displayed_user->domain;
$tab_array['parent_slug'] = bp_core_get_userlink(bp_loggedin_user_id());
$tab_array['css_id'] = 'jobs';
$tab_array['position'] = 100;
$tab_array['user_has_access'] = '1';
$tab_array['screen_function'] = 'bp_jobs_screen_general';
$bp->bp_nav['jobs'] = $tab_array; //Add new array element to the 'bp_nav' array
}
The 'screen_function' is a function that handles the screen that is to be shown when 'Jobs' tab is selected so you must add the function 'bp_jobs_screen_general' in functions.php:
function bp_jobs_screen_general() {
bp_core_load_template( apply_filters( 'bp_jobs_screen_general','members/single/jobs' ) );
}
This function looks for a template file named jobs.php in members/single/ so you must create it. For an example on how the screen_function's work, refer to a function for displaying Groups screens within wp-content/plugins/buddypress/bp-groups/bp-groups-screens.php.