URL Query on Wordpress - wordpress

I am applying specific styles in Worpdress when I access a certain URL, for example:
misiito.com/?mobile
When I enter there I hide the header and footer. My code is the following:
if ( isset( $_GET['mobile'] ) ) {
wp_enqueue_style( 'adaptabilidad', plugins_url( 'css/mobile.css', __FILE__ ), array(), '1.0.0' );
}
if ( isset( $_GET['totem'] ) ) {
wp_enqueue_style( 'adaptabilidad', plugins_url( 'css/totems.css', __FILE__ ), array(), '1.0.0' );
}
My problem is that it doesn't change the links and places the /?mobile, please help me, how can I do it?
That is, I want all the buttons at the end to have /?mobile
I tried this, but it's obviously a bug:
add_filter( 'the_content', 'adaptabilidad_nattule_add_mobile_url' );
function adaptabilidad_nattule_add_mobile_url( $content ) {
$content = str_replace( 'href="', 'href="?mobile', $content );
return $content;
}

It's just a suggestion, but have you thought of creating duplicate pages (for each styles) with different URL slugs and simply using a plugin like "Simple 301 Redirects" to associate them with a "/something"?
Example:
yousite.com/page1-style2
yousite.com/page1-style27

Related

i want to display images of my custom plugin in carousel style

i have made my first custom plugin which displays image gallery through shortcode. currently images are displayed randomely. i want to display them in carousel style by the use of owl carousel.
i have tried by including css and js files of owl carousel in my custom plugin folder. may be my scripts and styles are not getting called.i need help how to add loop code in my custom code. please guide me step by step as i am beginers in custom plugin development.
this is meta from where gallery is getting fetched and scripts and styles are included, please have a look if its proper.
function gallery_metabox_enqueue($hook) {
if ( 'post.php' == $hook || 'post-new.php' == $hook ) {
wp_enqueue_script('gallery-metabox', plugin_dir_url( __FILE__ ) . '/js/gallery-metabox.js', array('jquery', 'jquery-ui-sortable'));
wp_enqueue_script('gallery-metabox', plugin_dir_url( __FILE__ ) . '/js/owl.carousel.min.js', array('jquery', 'jquery-ui-sortable'));
wp_enqueue_script('gallery-metabox', plugin_dir_url( __FILE__ ) .'/css/owl.css', array(), null, 'all' );
/*wp_enqueue_style( 'owl-carousel' );*/
wp_enqueue_style( 'owl.css', get_stylesheet_uri() );
}
}
add_action('admin_enqueue_scripts', 'gallery_metabox_enqueue');
-----------------------------------------------------------------------------
this is shortcode from where gallery will be displayed in page where shortcode is pasted.
add_shortcode( 'banners', 'display_banners' );
function display_banners($atts){
$meta = get_post_meta($atts['id'],'vdw_gallery_id', true);
?>
<div id="owl-demo" class="owl-carousels">
<?php
foreach ($meta as $key => $value) {
$attpost= get_post($value); // declare post variable
echo '<img class="items" src="'.$attpost->guid.'" />';
}
?>
</div>
<?php
extract(shortcode_atts(array('key' => 'vdw_gallery_id'), $atts));
if($key && array_key_exists($key, $meta)) {
return $meta[$key][0];
}
exit;
}
?>
You can replace with
from : wp_enqueue_script('gallery-metabox', plugin_dir_url( __FILE__ ) .'/css/owl.css', array(), null, 'all' );
/*wp_enqueue_style( 'owl-carousel' );*/
To : wp_enqueue_style('gallery-metabox', plugin_dir_url( __FILE__ ) .'/css/owl.css', array(), null, 'all' );
/*wp_enqueue_style( 'owl-carousel' );*/
And make sure that you are able to get css and js in source code (ctrl+u)
You can get image object using below code
$images = get_post_meta($post->ID, 'vdw_gallery_id', true);
foreach ($images as $image) {
$image_obj = get_post($image);
echo $image_obj->post_excerpt;
}

Wordpress plugin development - Load the enqueued files only where we need them

I have developed 2 Wordpress plugins and I'm trying to load the needed css and js files in admin only where I need them.
I have written a function:
function ds_shortcodes_enqueue() {
$shortcodes_pages = array(
"shortcodes_plugin",
"add_shortcode",
"edit_shortcode"
);
$the_page = isset($_GET['page']);
if(in_array($the_page,$shortcodes_pages)){
// enqueue all our scripts
wp_enqueue_style( 'ds-shortcodes-style', plugins_url( '/admin/css/shortcodes-style.css', __FILE__ ) );
wp_enqueue_script( 'ds-shortcodes-script', plugins_url( '/admin/js/shortcodes-scripts.js', __FILE__ ) );
}
}
add_action( 'admin_enqueue_scripts', 'ds_shortcodes_enqueue' );
The function shoud load the files only on the pages with the slugs mentioned in the array.
Then, I have written a second plugin. Different name, different text domain, different functionality and I have used the same function:
function ds_videos_enqueue() {
$videos_pages = array(
"videos_plugin",
"add_video",
"edit_video",
"edit_video_category",
"video_categories",
"edit_video_level",
"video_levels",
"video_shortcode"
);
$current_page = isset($_GET['page']);
if(in_array($current_page,$videos_pages)){
wp_enqueue_style( 'ds-videos-style', plugins_url( '/admin/css/videos-style.css', __FILE__ ) );
wp_enqueue_script( 'ds-videos-script', plugins_url( '/admin/js/videos-scripts.js', __FILE__ ) );
}
}
add_action( 'admin_enqueue_scripts', 'ds_videos_enqueue' );
Now here's the problem.
They load the files from both plugins on any plugin page in admin.
I just don't get it.
I couldn't find any way to fix this.
It seems in_array() returns always true.
I hope you can help.
Thank you.
admin_enqueue_scripts passes a page hook to the callback function.
So, you can do something like this,
function ds_shortcodes_enqueue($hook) {
if($hook != 'page_where_you_want_scripts') {
return;
}
wp_enqueue_style( 'ds-shortcodes-style', plugins_url( '/admin/css/shortcodes-style.css', __FILE__ ) );
wp_enqueue_script( 'ds-shortcodes-script', plugins_url( '/admin/js/shortcodes-scripts.js', __FILE__ ) );
}
add_action( 'admin_enqueue_scripts', 'ds_shortcodes_enqueue' );
Reference
I have also tried this but the in_array doesn't seem to work.
function ds_shortcodes_enqueue() {
$shortcodes_pages = array(
"shortcodes_plugin",
"add_shortcode",
"edit_shortcode"
);
if(in_array("shortcodes_plugin", $shortcodes_pages)){
// enqueue all our scripts
wp_enqueue_style( 'ds-shortcodes-style', plugins_url( '/admin/css/shortcodes-style.css', __FILE__ ) );
wp_enqueue_script( 'ds-shortcodes-script', plugins_url( '/admin/js/shortcodes-scripts.js', __FILE__ ) );
}
add_action( 'admin_enqueue_scripts', 'ds_shortcodes_enqueue' );
It should enqueue the 2 files only on the page with the slug "shortcodes_plugin". It loads the files everywhere.
There is a global variable in wp-admin called $pagenow which holds name of the current page, ie edit.php, post.php, etc.
You can also check the $_GET request to narrow your location down further, for example:
function ds_shortcodes_enqueue() {
$shortcodes_pages = array(
"shortcodes_plugin",
"add_shortcode",
"edit_shortcode"
);
if ( isset($_GET['page']) ) {
global $pagenow;
if(in_array($pagenow,$shortcodes_pages)){
// enqueue all our scripts
wp_enqueue_style( 'ds-shortcodes-style', plugins_url( '/admin/css/shortcodes-style.css', __FILE__ ) );
wp_enqueue_script( 'ds-shortcodes-script', plugins_url( '/admin/js/shortcodes-scripts.js', __FILE__ ) );
}
}
}
add_action( 'admin_enqueue_scripts', 'ds_shortcodes_enqueue' );
function ds_videos_enqueue() {
$videos_pages = array(
"videos_plugin",
"add_video",
"edit_video",
"edit_video_category",
"video_categories",
"edit_video_level",
"video_levels",
"video_shortcode"
);
if ( isset($_GET['page']) ) {
global $pagenow;
if(in_array($pagenow,$videos_pages)){
wp_enqueue_style( 'ds-videos-style', plugins_url( '/admin/css/videos-style.css', __FILE__ ) );
wp_enqueue_script( 'ds-videos-script', plugins_url( '/admin/js/videos-scripts.js', __FILE__ ) );
}
}
}
add_action( 'admin_enqueue_scripts', 'ds_videos_enqueue' );
It seems in_array() returns always true.
<?php
$videos_pages = array(
'videos_plugin',
'add_video',
'edit_video',
'edit_video_category',
'video_categories',
'edit_video_level',
'video_levels',
'video_shortcode'
);
$current_page = isset($_GET['page']);
if(in_array($current_page,$videos_pages,true)){
echo 'I am debug point';
}else{
echo 'I am debug point 2';
}
exit;
?>

How to add custom stylesheet to TinyMCE in WordPress via a plugin?

I am trying to add a custom stylesheet to the TinyMCE editor in a WordPress plugin I am developing. The WP codex tells me to use the mce_css filter, but it does not seam to work.
As soon, as I use the filter, all the theme's custom stylesheets disappears from the editor, but my custom stylesheet is still not there.
See the following two screenshots, the first without the filter, the second with the filter activated:
Here is my code:
class test_plugin {
function __construct($args = array()){
if ( is_admin() ){
add_action('admin_head', array( $this, 'admin_head') );
add_action( 'admin_enqueue_scripts', array($this , 'admin_enqueue_scripts' ) );
}
}
function admin_head() {
if ( !current_user_can( 'edit_posts' ) && !current_user_can( 'edit_pages' ) ) {
return;
}
if ( 'true' == get_user_option( 'rich_editing' ) ) {
add_filter( 'mce_css', 'plugin_mce_css' );
}
}
function admin_enqueue_scripts(){
// wp_enqueue_style('fa_icon_shortcode', plugins_url( 'css/mce-button.css' , __FILE__ ) );
}
function plugin_mce_css( $mce_css ) {
if ( ! empty( $mce_css ) )
$mce_css .= ',';
$mce_css .= plugins_url( 'editor.css', __FILE__ );
return $mce_css;
}
}
new test_plugin();
Any idea what goes wrong here?
It actually was a pretty simple solution. Since I am using OOP, I had to add the filter with
add_filter( 'mce_css', array( $this , 'plugin_mce_css' ) );
instead of
add_filter( 'mce_css', 'plugin_mce_css' );
Works like charm!

Automatically Deactivate Plugin After a Check

I'm trying to get my Wordpress plugin to deactivate automatically after a simple check. It seems to be calling the admin_notices method just fine, but the deactivate_plugin() method does not do anything. This is in the class constructor:
// End if the theme isn't compatible
if ( FALSE == $this->themesupport['support'] ) { // This test works fine
add_action( 'admin_init', array( &$this, 'deactivate_plugin' ) ); // Plugin doesn't deactivate
add_action( 'admin_notices', array( &$this, 'admin_notices' ) ); // I get notices
if ( isset( $_GET['activate'] ) )
unset( $_GET['activate'] );
return;
} // if()
The method is pretty straightforward:
public function deactivate_plugin() {
deactivate_plugins( plugin_basename( __FILE__ ) );
} // deactivate_plugin()
Putting an echo in that deactivate_plugin method and it gets called. I've also tried including the plugins.php file from core with no change.
The following works:
<?php
/**
* Plugin Name: (SO) Self-deactivate with $_GET['my_deactivate']
*/
add_action( 'admin_init', function()
{
if( isset( $_GET['my_deactivate'] ) )
{
deactivate_plugins( plugin_basename( __FILE__ ), true );
$url = admin_url( 'plugins.php?deactivate=true' );
header( "Location: $url" );
die();
}
});
After activating, enter any admin URL adding ?my_deactivate, e.g.:
http://example.com/wp-admin/users.php?my_deactivate
Reference:
Function deactivate_plugins does not exist
I was calling deactivate_plugins() from within a class, rather than the plugin file itself, which of course returned the wrong location for FILE

Wordpress : Conditional Tags Within Plugin

Why can't use conditonal tags inside plugin? Here's the script:
/* enqueue */
function portfoliodetail_enqueue() {
wp_enqueue_script( 'portfoliodetailjs', plugin_dir_url(__FILE__) .'js/portfolio-detail.js', array('velocity'), null, true );
}
if ( is_singular( 'portfolio' ) ) {
add_action( 'wp_enqueue_scripts', 'portfoliodetail_enqueue' );
}
/* /enqueue */
the is_singular( 'portfolio' ) (because the post type is portfolio) works well in theme directory file. But when it is written within plugin, it doesn't work.
Anybody willing to help?
Your condition should be in the hook.
it should be like this:
function portfoliodetail_enqueue() {
if ( is_singular( 'portfolio' ) ) {
wp_enqueue_script( 'portfoliodetailjs', plugin_dir_url(__FILE__) .'js/portfolio-detail.js', array('velocity'), null, true );
}
}
add_action( 'wp_enqueue_scripts', 'portfoliodetail_enqueue' );

Resources