I'm trying to set up an autosave for dynamic wp editors in case my users forget to click save. (Currently set up with AJAX button to save.) Users can have 0 to 20 separate entries.
It seems that something should be added to the wp_editor settings array so it is added when tinymce is initialized. the The settings array is workinG. I can add or remove media buttons. However, I can't seem to get the right code inserted into the settings to get it to fire something when the active editor is changed.
Here is what I have.
$editorSettings =
array ( 'media_buttons' => false,
'resize' => false,
'wp_autoresize_on' => true,
'setup' => "function(ed) {
ed.onChange.add(function(ed, l) {
console.debug('Editor contents was modified. Contents: ' + l.content);
});
});"
);
Building editor with this code.
<?php wp_editor( stripslashes($j->journal_entry), 'journal-edit-'.$i , $editorSettings ); ?>
Ideally I would just run the AJAX save code I have already written. However, the change event does not appear to be firing.
Is this the best way to do this or should I create something outside of wp_editor functions. I can find lots of code to manually start tinymce and modify it but not when trying to go through Wordpress.
I've thought about setting up hidden fields to compare but this seems like a bad way to go. There should be some build in functionality to utilize instead of creating it from scratch.
Thank you for your help and suggestions!
So it looks like you do not want to do this through the settings array. Instead, add a filter to insert this code pre_init. The following code "clicks" a save button and prints to console on change event from tinymce in wordpress.
The setup js string needs to be minified or it will throw errors in console.
Add something like this to your functions.php file. NOTE: ' needed to be escaped in some of this code so it did not error out the functions file.
function mce_autosave_mod( $init ) {
$init['setup'] = 'function(a){a.on("change",function(b){jQuery(this).parent().find(\'#btnEditCharJournal\').trigger("click"),console.log("the event object ",b),console.log("the editor object ",a),console.log("the content ",a.getContent())})}';
return $init;
}
add_filter('tiny_mce_before_init', 'mce_autosave_mod');
All kinds of other code could be thrown in here. It is a good idea to also add logic to limit this to the specific editor on your site. You don't want this running for all of the instances of the editor.
If you have any suggestions to clean this up or do it a better way please let me know. I am fairly new to filters and wp_editor/tinymce.
Related
I'm using cefsharp and vb.net to put some code together to move to the next page of this site:
https://www.recommendedagencies.com/search#{}
The idea is to read the list of company names on each page and store to a csv file. This part I can do.
The problem I have is that I can't find the name of the 'Next' button - presumably if I had that I could execute some javascript on the browser to press the button.
I've inspected the page in Firefox, but can't see any name I can use - I'm not really familiar enough with html/page design to know why not or how it works.
Could anyone tell me a good method to get button names from a web page? - I've done some searching and even asked a similar question myself before, but I can't find anything which helps, given my patchy knowledge.
Thanks
Inspect the DOM for the 'Next' button.
Look for id's or classes that you can use to identify it.
use document.querySelector() to find the element by the css selector
call the element.click() function to programatically press next
const nextButton = document.querySelector('.sp-pages-nav_next')
nextButton.addEventListener('click', (event) => {
console.log('something clicked next')
})
nextButton.click()
<div class="sp-pages-nav sp-pages-nav_next" data-reactid=".1.3.4.1">Next</div>
In the above snippet on load you can see the code nextButton.click() invokes the console log. You can click the word Next manually to the same effect.
in cefsharp perhaps something like:
browser.ExecuteJavaScriptAsync("(function(){ document.querySelector('.sp-pages-nav_next').click(); })();");
A very similar example can be found here :
https://github.com/cefsharp/CefSharp/wiki/General-Usage#1-how-do-you-call-a-javascript-method-from-net
// When executing multiple statements, group them together in an IIFE
// https://developer.mozilla.org/en-US/docs/Glossary/IIFE
// For Google.com pre-populate the search text box and click the search button
browser.ExecuteJavaScriptAsync("(function(){ document.getElementsByName('q')[0].value = 'CefSharp Was Here!'; document.getElementsByName('btnK')[0].click(); })();");
I have developed a WordPress plugin which renders some dynamic html and javascript content. I would like to know how I should modify it so that it can be hooked to a single page in my wordpress application.
An idea I had was to make a bind the plugin function to a custom hook, and call this hook on the page, however, I don't know how to call the hook from a specific page. Or hook it to the part of the page you want and then use a conditional statement to only render if i is the correct page? This seems annoying because I want to be able to use my plugin on any page without modifying it. Is there a way I can pass the desired page to it as an argument?
I am looking for the "best practice" solution so I apologize if the question is a bit broad.
If this will be a public plugin, you won't be able to "bake in" the pages/posts on which it's supposed to show up.
You'll either need to:
1) Add an options page of some sort. Take a look at The Option Page Docs for more information
2) Add a Meta Box to the post types you want it to apply to that has an option in it that sets a meta value on the post.
With the information provided, I'd lean towards #2, then you can just check if the meta value is set, and if so, display your plugin's code:
if( is_single() && get_post_meta( get_the_ID(), 'run_amin_hakem_plugin', true ) == true ){
// Do your thing
}
You could then add the above code to an appropriate hook, like wp_head, wp_footer, or wherever your output is supposed to be displayed.
There are multiple hooks that can help you with rendering the desired html/js. The very first that comes to mind are
wp_head
wp_footer
the_content
the_title
If there is a sidebar you can add widget control plugin and show a widget on certain page.
If you end up using any of the above mentioned hooks or any other hook, you can do conditional check using one of the following:
is_page_template\ Given that you know the custom template in use.
is_page \ You will need to know page id/slug or title
Then there are many checks that will check if it is loading single.php or any of the taxonomy etc.
Hope this answers your question.
This should be a good example of targeting based on post id
add_action( 'the_content', 'my_override_function' );
function my_override_function($content) {
global $post;
if ($post->ID == 123) {
return 'Hello my friend<br>'.$content;
} else {
return $content;
}
}
I have a problem here and it's making me mad. I maintain a legacy php system. It's badly organized, no frameworks are used and a lot of other problems, for example, at least 5 different query versions are used in different parts of the system.
So my problem right now is this, I have a search form, when a button is clicked it shows a list of items, lets call this a list of "A" objects. In each A item, there is an expand /toggle button to show the B items that belong to A (this is done using ajax, by setting a specific div's html to the ajax response). Then each B also has an expand/toggle button to show the C items that belong to B.
What is happening: I click to search, all A are shown. I click on the expand to show the B items of one A object, they are shown. I click B to hide it, it hides and then show again. If I click it one more time, it hides, shows and hide. So it is like every ajax request is including the javascript code and running it.
I think this is pretty much an organization problem, I am not knowing how to include/require and insert the js correctly. I've been trying to solve this issue since yesterday and I think I've seen the code so many times that I can't think out of the box.
So here's some of the organization (I changed the names because there are some business rules):
SearchResults.php -> Declares a class that has static methods to print out HTML of each item A, B and C and some other helper methods. To make stuff "work", it has a require "js.php"; otherwise the A expand button does not work because it does not exist at the time the js is executed and no function is bound.
search.php -> the HTML form with all the search options, nothing important.
js.php -> the javascript stuff, why is it in a ".php"? I can't even remember, but I think it is because with the php I can require/include:
<script type="text/javascript" src="../util/js/jquery/1.8.1/jquery.min.js"></script>
<script>
var jQ = jQuery.noConflict(true);
jQ(document).ready(function() {
jQ(".loadBfromA").click( function(e) {
if (div.style.display == 'none')
alert("was hidden, now is showing");
//call ajax_B.php
//response is put into the div using .html(data)
else
alert("was showing, now is hidden");
...
ajax_B.php -> the ajax that access the database and echoes html code that will be put into the A items div. Here I have to require SearchResults.php, because I call some methods of the class.
Why is it including the jQ(document).ready being executed multiple times? How can I fix it? Is there any way I can reorganize the code?
Is the ajax_B.php, when requiring SearchResults.php, including the js again because SearchResults.php requires js.php? Does this gets echoed and then put into the div?
I can't make a fiddle of this because there is ajax included.
Edit:
I have tried unbind("click").bind("click", ()) and it didn't work.
It looks like the event is being bound multiple times on jQ(".loadBfromA")
I know this is not the cleanest solution out there, but you could rewrite the actual binding:
jQ(".loadBfromA").bind('click.loadbfroma', function(e) {
// Do your code
$(this).unbind('click.loadbfroma');
});
That way you can at least be sure that only one event is bound at all times, no matter how many times the code snippet is being included. I know this doesn't really help you with the underlying issue but it's a start.
So I have an .aspx page loading up and populating drop downs based on a lookup table in the database. What we want is for the user to be able to configure which of these values comes defaulted by specifying that value in a drop down somewhere else in the app.
Now, it's easy to default the "first" value, or something like that, but we want this to act differently. Is there a JavaScript function that can run immediately after onLoad (so the drop downs are populated already), that can go through the drop down and make one of those default so it looks like the page is loading with that set as default and people don't realize the workaround? (weird masking, etc.)
I'm not TOO experienced with JavaScript so something like this may already exist.
Here is an example of how we add the dropdown control to the page, in case you guys even need that.
With CType(.AddControl(New Controls.ComboBox("CodeId", "../../../../CodeId", "Code")), Controls.ComboBox)
.ForceSelection = True
.ValueField = "LookupID"
.DisplayField = "LookupDesc"
.Validate.AllowBlank = False
.ForceSelection = True
.ReadOnly = EditControl.IsFieldReadOnly(10580)
.BindData(Model.Codes)
End With
Thanks guys!
-Scott
Since you are using ExtJs you can do something like:
Ext.onReady(function(){
//place your code here
});
This may solve your problem
You need an onReady event, like what jQuery provides: http://api.jquery.com/ready/. It will run when the page content is ready to be manipulated.
Details:
I'm basically trying to implement the functionality of the example here (http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/editondblclick/defaultvb.aspx) on my own site, but instead of using a data source control located in the page markup (like in the example: SessionDataSource), I'm using a dataset that I get from some server code-behind. I am able to successfully get my double-clicked row into edit mode with my dropdowns successfully populated, however when clicking onto another row to update the one in edit mode, no-dice.
I've discovered that apparently the client-side JavaScript function updateItem() does not initiate an AJAX callback like I originally thought, so I've been trying to work my way around that. Currently I'm using my RadAjaxManager to perform an AJAX callback with the row index number like so:
function RowClick(sender,eventArgs)
{
if(editedRow && hasChanges)
{
hasChanges = false;
$find("<%= RAM.ClientID %>").ajaxRequest(editedRow);
}
}
This gets me to my server code. Awesome. But,
Problem: I must be accessing something wrong, because the GridDataItem cell text that I'm trying to obtain for the edited row all has the value " ".
Dim gdi As GridDataItem = FieldOpsScheduler.Items(rowIndex)
Dim d As DateTime = DateTime.Parse(gdi.Item("EndDate").Text) //<--FAIL
I've been scouring the Internet for hours now trying to find how I can pull off what I'm trying to do, but to no avail.
Additional Info: I'm using GridDropDownListColumnEditors on the front-side to do the editing for my table, declared like so:
<telerik:GridDropDownListColumnEditor ID="ddlce_SunAct" runat="server" DropDownStyle-Width="60px"></telerik:GridDropDownListColumnEditor>
So does anybody have any ideas on what I have to do to access the values that have been changed in my RadGrid?? Is the problem that I somehow need to rebind my RadGrid when clicking on a new row? If so how do I do that? Any solutions or ideas would be greatly appreciated. (Also although I'm doing this in VB.NET, feel free to write responses in C# if you wish as I understand that too. :-) ) Thank you in advance.
With manual binding updateItem() should still raised the UpdateCommand server event of the grid, but you will have to update the grid source by hand. Modify the local version of the online demo where you Telerik AJAX controls installation is and go from there.
Dick
http://www.telerik.com/help/aspnet-ajax/grdaccessingcellsandrows.html
If anyone else has the same problem as I did, go to the above link and scroll down to the section titled "Accessing the value of cells in edit mode".