What I'm looking to do is create a very basic plugin for Wordpress. I've followed countless tutorials and examples, but haven't found anything close to what I'm looking for.
I'm trying to create something simple where it has a link on the admin nav bar, and just shows some static html on the right side of the page. The public wouldn't see anything at all.
This sounds like a simple task, but so far, has been everything but simple. Any help or pointing in the right direction would be appreciated. :)
Create a folder named helloworld in your plugins folder and add a file called helloworld.php. In your PHP file, use a hook to add the link to the admin menu.
<?php
/*
Plugin Name: Hello World
*/
add_action('admin_bar_menu', 'add_navbar_item', 100);
function add_navbar_item($admin_bar){
$admin_bar->add_menu( array(
'id' => 'my-item',
'title' => 'My Item',
'href' => '#',
'meta' => array(
'title' => __('My Item'),
),
));
}
Related
I am trying to create a Drupal module. I've been able to setup the configuration form on the admin section - it runs fine: I can add the component and set configurations and save.
However, nothing appears on the front end of the site. There are no errors. I'm not sure why and, as I am new to Drupal, I'm not sure where to look.
My hook_theme in my .module file looks like:
function gallery_grid_theme($existing, $type, $theme, $path) {
return array(
'gallery_grid' => array(
'template' => 'gallery-grid',
'path' => 'plugins/content_types/gallery_grid/templates',
'type' => 'theme',
'variables' => [],
)
);
}
The .tpl file is intact and has no markup errors.
Would anyone know what file I should be looking at?
EDIT:
I've tried clearing the cache and rebuilding the registry as well as disabling and re-enabling the module, to no affect.
The module is added to a page panel as a component (gear icon, Add Content).
For some reasons this needed an absolute path:
'path' => drupal_get_path('module', 'gallery_grid') . '/plugins/content_types/gallery_grid/templates',
I am trying to remove numbers from pagination links on a paginated Wordpress post to leave only next/previous buttons. I have the code as follows and it still does not work as required. If I set a display:none; css rule to the tags within the .paging p class this affects all links including the next/previous links as the links do not have a separate class to them.
<?php
wp_link_pages(array(
'before' => '<p class="paging" style="margin-bottom: 5em;">' . __(''),
'after' => '</p>',
'next_or_number' => 'next', # activate parameter overloading
'nextpagelink' => __('<span class="pagelink right">NEXT</span>'),
'previouspagelink' => __('<span class="pagelink left">PREVIOUS</span>'),
'pagelink' => '%',
'echo' => 1 )
); ?>
Here is an example of a post illustrating the problem: http://famtrav.staging.wpengine.com/destinations/uk/15-fun-things-july-2016/
Is there another way of me achieving the required result? I hope this makes sense. Many thanks.
I have now solved this. The problem was caused by conflicting code. Specifically the wp_link_pages function was being defined both in the themes functions.php file and in the post-template.php file within the /wp-includes/ folder with instructions in one incidence contradicting the other. I commented out the instructions in the functions.php file and altered the post-template.php file with the correct code and this solved the problem.
I am using the Meta Box plugin for Wordpress. I can successfully create fields in the cms for users to upload images. I would like to extend this in two ways:
First, I would like a delete confirmation when users remove an image from the image gallery
Here is the code:
$meta_boxes[] = array(
'id' => 'project_media',
'title' => 'Project Media',
'pages' => array( 'project' ),
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Media Gallery',
'desc' => 'Images should be sized to 983px x 661px',
'id' => $prefix . 'project_media_gallery',
'type' => 'image'
)
);
This creates upload functionality in the custom post type where users can add images to a slideshow. The problem is if the user accidentally clicks the delete button, there is no confirmation to make sure it is deleted. Can I somehow extend the plugin through functions and call an alert when this button is clicked? Something that does not involve editing the WP core?
Second, the base functionality requires the user to upload an image from their local machine. Is there a way to tap into the Media Library for this?
No idea how to even start tackling this one.
To answer the first question
First, I would like a delete confirmation when users remove an image from the image gallery
You can do that by calling a custom script file from the functions.php.
function alert_delete() {
if(is_admin()){
wp_register_script( 'alert_delete', get_bloginfo('template_url'). '/js/alert_delete.js', array('jquery'));
wp_enqueue_script('alert_delete');
}
}
and create a file named alert_delete.js in the js directory of your theme.
alert_delete.js:
// admin delete check
jQuery(document).ready(function(){
jQuery(".rwmb-delete-file").click(function() {
if (!confirm("Are you sure? This process cannot be undone.")){
return false;
}
});
});
In response to the second question...
Second, the base functionality requires the user to upload an image
from their local machine. Is there a way to tap into the Media Library
for this?
Get the latest version of the Meta Box Plugin first.
then change
'type' => 'image'
to
'type' => 'image_advanced'
which will allow you to upload from the existing Media Gallery or a new file from your computer.
So my aim is to find a method of adding more thumbnails only for displaying on a custom post type, for example I wish to have a large image (not the same image) for a featured post and a different image for the default view.
In the end i followed this tutorial and it did exactly what i required to a T.
http://www.lifeonlars.com/wordpress/how-to-add-multiple-featured-images-in-wordpress
have you try this add_image_size
why don't you use custom post template plugin
I got a solution from online. I also customized some code. You can check this.
Step 1 Download this library from this link and put beside functions.php ( theme root ).
Step 2: Copy this code below to functions.php.
/*
* Code for Multiple Featured Image.
* Multiple Featured image is only for your selected post type.
*/
require_once('library/multi-post-thumbnails.php');
if (class_exists('MultiPostThumbnails')) {
new MultiPostThumbnails(array(
'label' => '2nd Feature Image',
'id' => 'feature-image-2',
'post_type' => 'your_post_type_name'
)
);
new MultiPostThumbnails(array(
'label' => '3rd Feature Image',
'id' => 'feature-image-3',
'post_type' => 'your_post_type_name'
)
);
new MultiPostThumbnails(array(
'label' => '4th Feature Image',
'id' => 'feature-image-4',
'post_type' => 'your_post_type_name'
)
);
};
Step 3 Check now.
I can write entire code here, but clicking on this tutorial link is much easier :)
Drupal 7 hook_menu() is confusing me; I have tried everything and I can't seem to get this to work.
What I need: In a custom module, I'd like to create a new menu, and add about four links to that menu. It sounds simple, but I am struggling. I've been able to create the menu itself using the $menu array in the .install file, but adding items to that menu doesn't make sense.
Code that is working:
$menu = array(
'menu_name' => 'project-menu',
'title' => $t('Project Menu'),
'description' => 'Project Menu',
);
menu_save($menu);
Code that isn't working:
$items = array();
$items['project-menu/%'] = array(
'title' => 'Test Link',
'page callback' => 'dc_project_page',
'page arguments' => array(1),
'access callback' => TRUE,
'type' => MENU_LOCAL_TASK,
);
return $items;
This is all in the dc_project.install file under the dc_project_menu() function. Hopefully I'm just doing something stupid, any and all help is extremely appreciated. Even just pointing to me to a module that does this cleanly as an example, thanks. I did look at the example project, haven't been able to get anything as far as adding links to my new menu working.
Passing to menu_save() the content of $items doesn't work because menu_save() accepts only an array containing menu_name, title, and description.
What you use in $items is an array describing the menu callbacks implemented by a module, and the definitions of the menu callbacks implemented by all the modules are not saved in "menu_custom" (the table used from menu_save()) but are cached in a Drupal cache table.
If you are trying to change the menu callbacks defined by another module, then you should implement hook_menu_alter(); otherwise, if you just want do define the menu callbacks of your module, you should implement hook_menu().
Both the hooks implementations (hook_menu() and hook_menu_alter()) must be in the module file (in your case, in dc_project.module), not in dc_project.install. Drupal doesn't load the installation file when it normally loads the enabled modules; it loads the installation file when a module is being updated (or installed), but it doesn't load it in other cases.
The code that saves the menu with menu_save() can be in the installation file, in the implementation of hook_install() or hook_update_N(). It could also be put in the implementation of hook_enable(); in that case, the code (which is executed when the module is enabled) should first verify the menu has not been already added. (hook_enable() and hook_disable() should be placed in the installation file.)