Display the same author name for all users in WordPress? - wordpress

For a Wordpress organization website, I want all the posts to have by default the same author name (the organization name). How can I achieve this behavior?

There are multiple possibilites.
1) Simplest: You only create one author and share the login within the organization
2) You simply do not display the author on your post - why would you do that? It is probably obvious that your organization is the publisher of these pages anyway.
3) Add the following custom code within your Theme or Plugin:
add_filter('the_author','its_my_company');
function its_my_company() {
return 'Organization Name';
}
https://developer.wordpress.org/reference/hooks/the_author/

Your best best is to modify your theme or child theme to display a specific user or name wherever the template does so. In your single.php file, look for the_author() or get_the_author(). Then use something like get_user_by() to pull a specific user, or else hard code the value (less ideal, but an option).
Your other option is to manually set it each time, which I could exactly define as making anything "default." Even so, the option exists.

Related

What to do if a shortcode already exists

I'm writing a Worpress plugin that defines a shortcode.
What happens at shortcode registration if there's another shortcode already registered with that same name ?
What is the practice ? Is there a possibility to find the conflicting plugin so as to warn the user that he should deactivate it to be able to activate yours ??
Thanks.
if ( !shortcode_exists( 'myshortcode' ) ) {
add_shortcode('myshortcode','mycallback');
}
else ?????
The best practice is allways to prefix the name of your shortcode in order to avoid naming collisions. You can use a particular prefix in relation with your plugin name or activity. You can see some guideline in the plugins development handbook here : https://developer.wordpress.org/plugins/the-basics/best-practices/#prefix-everything
My personal practice is to process with a personal name spacing format. It could give something like:
add_shotcode('company_plugin_shorcode');
Hope it helps
Asking the user to disable another plugin just because you can't register your shortcode with the name you want might not be the best idea for a variety of reasons.
Other things you could try instead:
Name your shortcode something unique. For example, WooCommerce's shortcodes are all prefixed with woocommerce_: [woocommerce_cart], [woocommerce_checkout], et cetera.
You could register an alternative shortcode if the name you want to use is already taken.
If you ask me, I'd go for #1.

Place an Edit Button on the view node page in Drupal 7

I don't use the Drupal Tabs because they interfere with my CSS but I need the functionality of the Edit tab to be on that screen so that a user can edit the node after reviewing it.
Any ideas on how to do this? Functions? tpl placement? Thanks!
You can do this in a custom module as follows.
In yourcustommodule.module you implement hook_preprocess_node(). In there you check if the user has permissions to edit the node and you set the edit link.
function yourcustommodule_preprocess_node(&$vars) {
if (node_access("update", $vars['node']) === TRUE) {
$vars['edit_link']['#markup'] = l(t('Edit'), 'node/' . $vars['nid'] . '/edit');
}
}
In the node.tpl.php template in the theme you print the edit link if it is available.
<?php if (isset($edit_link)) : ?>
<p><?php print render($edit_link); ?></p>
<?php endif; ?>
If you do not have a node.tpl.php template in your theme folder than copy the one from modules/node.
If you're using the Views Format "Fields", one of the fields that you can add is "Edit Link." It's pretty flexible; it will tell you what text to display in the link. That's probably the preferred option.
If you're not using the "Fields" format, it gets trickier, especially since you're already interfering with some basic drupal styling. I'd need more information about your View and your skill set to recommend a method that doesn't cause more problems.
As a sidenote: I learned Drupal theming from the outside in, and used to use CSS that would interfere with the underlying drupal mechanics like tabs and contextual links. I've moved away from that; I find very few cases where I need to interfere with native styling-- and for those I can use custom .tpl's to get around.
EDIT: Ah. If you're not using views, a custom page .tpl is probably the best way to go. If you're not familiar, the structure for any node edit link is '/node/<NID>/edit' (for clean URL's) or '/?q=node/<NID>/edit' for old-style URL's. Depending on how your path aliases are set up, '/<url-alias>/edit' may work as well but the previous ones are more reliable.
This link on drupal.org gives a few options.
I think u can write a theme file(.tpl) for u specific case and theme page in whichever way u want

add custom field template to theme admin page

in WP admin how to add the custom field template plugin to a theme page?
it automatically shows in posts and pages but i want this in the theme page. the theme am using is from iwak "creations" portfolio page.
what files do i need to modify to add this?
It's very hard to say what you need to modify without being able to look at the code. Being a premium theme, we can't just download it and take a look.
Having said that, the theme may use the WordPress custom post type functionality. Search the code for a call to the register_post_type function. If it's used, you may be in luck. Either
add 'custom-fields' to the supports argument in that call, or
call add_post_type_support after the post type is registered. The $post_type parameter will be the first value passed to the register_post_type function, and the $supports parameter will be 'custom-fields'.
Daniel, are you using this Custom Post Type Plugin - http://wordpress.org/extend/plugins/custom-field-template/screenshots/? I've used it before, and the guy who created it is Japanese, so his personal page isn't very useful as far as support for english goes.
I had some trouble with this at first. But what I think you're trying to do is add the custom fields to your new pages you've created, correct?
It's not very straightforward, but once it works it's pretty awesome.
Basically, when you set up the plugin you create these different "Custom fields," right? Well part of that should look like this:
[Custom Field Name]
type = textarea
rows = 4
cols = 40
tinyMCE = true
htmlEditor = true
Ok, so once you've created those "Custom fields" keep note of the part in brackets. You'll add this to your template pages:
<?php getCustomField('Custom Field Name'); ?>
Now when you enter the info in the pages or posts, the content should appear as you've entered it.

disable delete media wordpress

Here's what I've been trying to figure out how to do for hours now.
I want the author role to be able to upload content into the media library from their posts, and to be able to view the entire media library. What I don't want is to let the author role delete any media, even their own.
I've thought about automatically switching authors to a 'media' user upon upload completion, but I was hoping there'd be a cleaner way.
Any ideas?
There's no built-in capability for "delete_media." I think this is encompassed by "delete_posts," since uploads are treated as posts. (Note that authors can delete only their own posts and attachments.)
Adding a role or capability is possible, but you'd have to replace the default media admin screens, where the delete action is controlled by the "delete_posts" cap. You don't want to mess with the core files upload.php etc. So you'd have to restrict access to them, then write your own panels for authors. You probably don't want to do this. :-)
There's a better way, however. If you assign uploads to an administrator when they are saved then authors will not be able to delete them. You can use the add_attachment and edit_attachment action hooks to change the post_author to an administrator id.
Hope that helps.
There are a number of places where WordPress will let users delete images, so trying to hide them all can be challenging (and dangerous, because a new plug-in or version of WordPress could add another). However, you can add the following function to prevent deletions and throw an error (not pretty, but effective). You could enhance this by adding a custom capability for deleting images, if you wanted a finer level of control.
add_action('delete_attachment', 'DontDeleteMedia', 11, 1);
function DontDeleteMedia($postID)
{
if (!current_user_can('manage_options')) {
exit('You cannot delete media.');
}
}
add this code in your functions.php file:
add_action('media_row_actions','users_own_attachments', 2, 1);
function users_own_attachments( $wp_query_obj ) {
if( !current_user_can( 'delete_plugins' ) ){
unset($wp_query_obj['delete']);
return $wp_query_obj;
}
}

Change management in WordPress

I have a beginner question. What is the best way to address the change management issues in WordPress? I have an all-pages WordPress installation. Suppose name of some event or an entity changes from A to B, then I have to go to all the pages to make that change. Is there any better way of doing it? Like externalization or something.
Or the way similar to how WordPress handle blog name using bloginfo() function. You change blog name at one place and it is reflected everywhere.
Thanks,
Paras
If a URL on your site changes, it is always wise to leave a redirect to the new page. This will help your visitors and search engines. If you create redirects, it doesn't matter too much if you still have a link to the old address in one of your posts. There will probably be a plugin for this, but I don't know which one.
If you really want to keep all links pointing to the latest version, you could replace them with shortcodes that are evaluated to the real URL. <a href="[linkto postid=123]"> would then result in <a href="/2010/08/05/some-post">. I think this is doable, but I don't know whether a plugin already exists for this.
You can also use this technique to replace short snippets, like your company name. The Shortcode API is really easy:
// [company_name]
function replace_company_name($atts) {
return "My Awesome Company";
}
add_shortcode('company_name', 'replace_company_name');
// More generic
// [replace r='company_name']
// [replace r='company_motto']
function do_replacement($atts) {
$replacements = array(
'company_name' => 'My Awesome Company',
'company_motto' => 'A Company so Awesome even you would want to work here!',
);
return $replacements[$atts['r']];
}
add_shortcode('replace', 'do_replacement');
You can hardcode the strings in your plugin code, or you could create a Wordpress options page where users can add and edit new shortcodes.

Resources