I created a plugin for WordPress. It opens a dialog box with an input field to fill, and then it creates a div with the contents of the field. Once the div was created in the editor, I would like to be able to doubleclick on this div and open the dialog box again.
How should I do it ?
Here, my PHP FILE :
<?php
/*
Plugin Name: my Plugin
*/
?>
<?php
function add_myItems(){
?>
<!-- the Dialog Box -->
<div id="myDialog">
<style type="text/css">
#myDialog {
display: none; /* hidden by default */
position: fixed;
z-index: 100; /* Sit on top */
width: 200px;
height: 200px;
}
</style>
<input id="myData" value="" />
OK
</div>
<!-- the WP button -->
Add DIV
<?php
}
function add_myJS() {
wp_enqueue_script('myJS', '/wp-content/plugins/my/script.js', array('jquery'), '1.0', true);
}
add_action('wp_enqueue_media', 'add_myJS');
add_action('media_buttons', 'add_myItems');
?>
...and Here, my JS FILE :
$('#myButton').click(function () {
$('#myDialog').show();
});
$('#myOK').click( function (){
var data = $('#myData').val();
var result ='<div class="myResult">' + data + '</div>';
wp.media.editor.insert(result); /* the WordPress function */
$('#myDialog').hide();
});
Thank you for your help. Nicolas.
Related
I am trying to modify the css of a SVG image(the logo) in a header.php of a Wordpress custom theme.
Here is the code in header.php:
<!-- logo -->
<a class="logo" href="<?php echo home_url(); /* insert home link */ ?>">
<img src="<?php echo get_stylesheet_directory_uri(); /* add theme path */ ?>/img/logo.svg" alt="<?php bloginfo('title'); /* insert blog title */ ?> ">
</a>
To modify the CSS of the SVG I have to inline it, but I don't know how to do, this SVG is saved from Illustrator.
I tried to add this jQuery script that transforms a SVG image in an inline SVG, but I don't the see the image in the browser, I see it if I inspect the element.
/**
* Replace all SVG images with inline SVG
*/
jQuery('img.svg').each(function(){
var $img = jQuery(this);
var imgID = $img.attr('id');
var imgClass = $img.attr('class');
var imgURL = $img.attr('src');
jQuery.get(imgURL, function(data) {
// Get the SVG tag, ignore the rest
var $svg = jQuery(data).find('svg');
// Add replaced image's ID to the new SVG
if(typeof imgID !== 'undefined') {
$svg = $svg.attr('id', imgID);
}
// Add replaced image's classes to the new SVG
if(typeof imgClass !== 'undefined') {
$svg = $svg.attr('class', imgClass+' replaced-svg');
}
// Remove any invalid XML tags as per http://validator.w3.org
$svg = $svg.removeAttr('xmlns:a');
// Replace image with new SVG
$img.replaceWith($svg);
}, 'xml');
});
Thanks for all the support.
Carlo
There are ways to style svg with attributes like fill, stroke or stroke-width for example. This is very usefull if you create you image actualy by code, for example:
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
Sorry, your browser does not support inline SVG.
</svg>
You have an svg image file and use it as src in your <img> tag.
It is not the direct answer to your question, but maybe it solves your problem, if you save the other version of your image also as an svg file (should be less than 10kb, so no performance problems) and change the src value.
In the comment I read you want to achieve a dark / light mode and also your logo image should change if another mode is selected, like:
document.getElementById("my_image_id").src = "my_image_file.svg";
Another solution would be to do all the look changes via the css and just set the data-theme with javascript. CSS will make the rest for you.
You could show your logo as a background-image, using the a parent:
<a class="logo" href="<?php echo home_url(); ?>">
<!-- here comes the logo -->
</a>
and in your CSS:
.logo {
display: inline-block;
width: 50px; /*your dimensions*/
height: 50px;
background-image: url(my_image_file.svg);
background-size: cover;
}
How to create dark / light mode without jQuery and minimum Javascript
This is more than you asked for, but maybe can help your task make more easy. You can give your html element an attribute, for example called data-theme and your button only changes this attribute value with javascript.
So in the html: You change the data-theme value with your toggle button (with a nice transition).
<html lang="en" data-theme="light">
<body>
.
.
.
<input type="checkbox" id="switch" name="theme" /><label for="switch">Toggle</label>
.
.
.
<script>
var checkbox = document.querySelector('input[name=theme]');
checkbox.addEventListener('change', function() {
if(this.checked) {
trans()
document.documentElement.setAttribute('data-theme', 'dark');
} else {
trans()
document.documentElement.setAttribute('data-theme', 'light');
}
})
let trans = () => {
document.documentElement.classList.add('transition');
window.setTimeout(() => {
document.documentElement.classList.remove('transition');
}, 1000)
}
</script>
</body>
</html>
That's all for the javascript and html part of the job. The rest is CSS with normal variables, you set once and use in your code.
html {
--bg: #AEAEAE;
--color-headings: #666;
--logo-source: url(my_light_logo.svg);
}
html[data-theme='dark'] {
--bg: #333333;
--color-headings: #FFF;
--logo-source: url(my_dark_logo.svg);
}
body {
background-color: var(--bg);
}
h1 {
color: var(--color-headings);
}
.logo {
background: var(--logo-source);
}
html.transition,
html.transition *,
html.transition *:before,
html.transition *:after {
transition: all 750ms !important;
transition-delay: 0 !important;
}
This way you change everything about the styling in your css, and only trigger the data-theme change with javascript on your button. For me, this seems to be the more logic way to achive light / dark mode, as CSS is there just for styling.
Hope it helps and maybe is something of interest for you!
.main-header {
background: url(logo.svg) no-repeat top left;
background-size: contain;
}
.no-svg .main-header {
background-image: url(logo.png);
}
something like this.. with the help of CSS.
I have this codepen which is an altered version of the parallax code from firewatch.
https://codepen.io/Lancewalker/pen/EGqVZe
function castParallax() {
var opThresh = 350;
var opFactor = 750;
window.addEventListener("scroll", function(event){
var top = this.pageYOffset;
var layers = document.getElementsByClassName("parallax");
var layer, speed, yPos;
for (var i = 0; i < layers.length; i++) {
layer = layers[i];
speed = layer.getAttribute('data-speed');
var yPos = -(top * speed / 100);
layer.setAttribute('style', 'transform: translate3d(0px, ' + yPos + 'px, 0px)');
}
});
}
document.body.onload = castParallax();
/* PARALLAX */
.body {
margin: -5px;
}
.keyart, .keyart_layer {
height: 1000px;
}
#parallax {
display: block;
}
.keyart {
position: relative;
z-index: 10;
}
.keyart_layer {
background-position: bottom center;
background-size: auto 1038px;
background-repeat: repeat-x;
width: 100%;
position: absolute;
}
.keyart_layer.parallax {
position: fixed;
}
#keyart-0 {
background-image: url("https://i.imgur.com/1faPCRE.png");
background-color: #ffaf1b;
}
#keyart-1 {
background-image: url("https://i.imgur.com/EKhFCWk.png");
}
#keyart-2 {
background-image: url("https://i.imgur.com/o4PVCnk.png");
}
#keyart-3 {
background-image: url("https://i.imgur.com/bnVtpuR.png");
}
#keyart-4 {
background-image: url("https://i.imgur.com/bnVtpuR.png");
}
#keyart-5 {
background-image: url("https://i.imgur.com/R5jRdBC.png");
}
#keyart-6 {
background-image: url("https://i.imgur.com/ORZm7M7.png");
}
#keyart-7 {
background-image: url("");
}
#keyart-8 {
background-image: url("");
}
#keyart-scrim {
background-color: #ffaf00;
opacity: 0;
}
<div class="keyart" id="parallax">
<div class="keyart_layer parallax" id="keyart-0" data-speed="2"></div> <!-- 00.0 -->
<div class="keyart_layer parallax" id="keyart-1" data-speed="5"></div> <!-- 12.5 -->
<div class="keyart_layer parallax" id="keyart-2" data-speed="11"></div> <!-- 25.0 -->
<div class="keyart_layer parallax" id="keyart-3" data-speed="16"></div> <!-- 37.5 -->
<div class="keyart_layer parallax" id="keyart-4" data-speed="26"></div> <!-- 50.0 -->
<div class="keyart_layer parallax" id="keyart-5" data-speed="36"></div> <!-- 62.5 -->
<div class="keyart_layer parallax" id="keyart-6" data-speed="49"></div> <!-- 75.0 -->
<div class="keyart_layer" id="keyart-scrim"></div>
<div class="keyart_layer parallax" id="keyart-7" data-speed="69"></div> <!-- 87.5 -->
<div class="keyart_layer" id="keyart-8" data-speed="100"></div> <!-- 100. -->
</div>
My goal is to have this as the header/intro image to my site.
I can easily implement this when a site is using simple html / css / JS, for instance I was able to quickly make it work on a friend's shopify site in just a minute or two.
With WPBakery though, the custom HTML element doesn't output the HTML / CSS in a way that overrides the WPBakery configs, so every single element I am creating with WPBakery has a base class that overrides things like width, height, padding, margins, etc based on whatever I choose in the configs. This is causing issues and my element is showing up as a blank space in the web page.
I cannot for the life of me figure out how to make this code work. I don't really know any PHP whatsoever, but am pretty good at front end development at this point. The more I work with CMS that let's me easily make changes in html / css / js the more wordpress is causing problems for me.
I installed a plugin that lets me add custom JS and CSS/SCSS, implemented the code there and then used the HTML output in the "custom html" element in WPBakery but it didn't work.
Any ideas?
At this point I'm comfortable enough to code my entire site just using an IDE and html / css / js.. I wish there was an easy way to add elements to wordpress easily that I create (is there?) without WPBakery causing so much difficulty.
When I reload the div content that time scrollbar is coming on top, I want when div reload that time scrollbar position same where I left.
<style type="text/css">
div.scrollbar {
height: 655px;
overflow: auto;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(function() {
function newPost() {
$("#newpost").empty().load("slide/parts/extruderRight1.php");
}
var res = setInterval(newPost, 30000);
});
</script>
// How to Add scroll bar maintain where I left load("slide/parts/extruderRight1.php")
<div class="voice {}"class="voice {}"><span class="label"><b>To Do List</b></a></span></div>
<div id="scrollbar" class="scrollbar">
<div id="newpost">
</div>
</div>
What I have to do so I can run proper or add anything into the my code i wanted to resume it to the previous scrolled position suggest me or any changes required I am stuck on this step
i have a code:
<body <?php if( in_category( 11446 ) ) { echo "style=\"background-image: url('my-background-url-of-image');background-repeat:repeat;\" onclick=\"window.open('http://www.domain.com');\""; } ?> >
This code works only until page loads fully than something happens and it doesn't work i assume from inspect element that onclick function changes and i'm failing to find what part tricks that.
What this code does is it sets unique body background that are in specific category and background is clickable.
But because of some javascript error it doesn't work when page loads full so maybe somebody could explain me how to remove attr on Javascript and than add my with domain i want. Or maybe give example how to do alternative code just with href.
Thank you.
I'm assuming you are building a Wordpress template and the background image to be used is based upon the category of a Wordpress post.
This uses no Javascript. Instead, what it does is create the CSS declaration block inside the head tag of the HTML5 document on the fly. It does no inline CSS for the body tag.
<?php
// ====================================================
// Solution #1
// Background Image Only
// ====================================================
function GetThisWordpressPostCategory() {
// Do post category to filename mapping here
return("cars");
}
function in_category() {
// Just a dummy to simulate Wordpress in_category call
return(true);
}
function BodyBackground($category) {
$bodyCss = "";
if (in_category($category)) {
$bodyCss =<<<CSS
<style type="text/css">
body {
background-image: url('{$category}.jpg');
}
</style>
CSS;
}
return($bodyCss);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Category background</title>
<?php
$category = GetThisWordpressPostCategory();
echo BodyBackground($category);
?>
</head>
<body>
</body>
</html>
<?php
// ====================================================
// Solution: 2
// Clickable div spanning viewport
// ====================================================
function GetThisWordpressPostCategory() {
// Do post category to filename mapping here
return("cars");
}
function in_category() {
// Just a dummy to simulate Wordpress in_category call
return(true);
}
function PageCss($category) {
$pageCss = "";
if (in_category($category)) {
$pageCss =<<<CSS
<style type="text/css">
html, body, #page {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
background-image: url('{$category}.jpg');
}
#page-anchor {
display: block;
width: 100%;
height: 100%;
}
</style>
CSS;
}
return($pageCss);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Category background</title>
<?php echo PageCss(GetThisWordpressPostCategory()); ?>
</head>
<body>
<div id="page">
<a id="page-anchor" href="http://www.google.com"></a>
</div>
</body>
</html>
i have a page like:
and i need to print to pdf ( or physical printer directly ) the content of yellow div.
I need to print the div like you see.. with css style too.. how can i do it? i see in other post that they print only text content..
Can someone help me?
You can assign different CSS properies for different device types.
For example:
<style type="text/css">
/* all devices */
#media all
{
#content { display:block;}
}
/* printer specific CSS */
#media print
{
#content { display:none;}
#content div#yellow { display:block;}
}
</style>
To print a page you can use javascript window.print() method:
<form>
<input type="button" value="Print this page" onClick="window.print()">
</form>
Use the following code:
<script type="text/javascript">
function CallPrint(strid) {
var prtContent = document.getElementById(strid);
var WinPrint = window.open('', '', 'letf=0,top=0,width=400,height=400,toolbar=0,scrollbars=0,status=0');
WinPrint.document.write(prtContent.innerHTML);
WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
WinPrint.close();
}
</script>
Print