![]() |
VOOZH | about |
The CData Cmdlets for WordPress are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to WordPress.
The Cmdlets are not only a PowerShell interface to WordPress, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete WordPress data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Wordpress. To access WordPress data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Wordpress.
Once you have acquired the necessary connection properties, accessing WordPress data in PowerShell can be enabled in three steps.
To connect to WordPress, set the URL property and other authentication properties. WordPress supports Basic (User and Password) and OAuth2.0 authentication, though Basic is recommended for a testing environment only. To connect with OAuth register an app with WordPress.
See the Getting Started guide in the CData driver documentation for more information.
Install the module:
Install-Module WordPressCmdlets
Connect:
$wordpress = Connect-WordPress -Url "$Url" -InitiateOAuth "$InitiateOAuth"
Search for and retrieve data:
$id = "1" $categories = Select-WordPress -Connection $wordpress -Table "Categories" -Where "Id = `'$Id`'" $categories
You can also use the Invoke-WordPress cmdlet to execute SQL commands:
$categories = Invoke-WordPress -Connection $wordpress -Query 'SELECT * FROM Categories WHERE Id = @Id' -Params @{'@Id'='1'}
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Wordpress\lib\System.Data.CData.WordPress.dll")
Connect to WordPress:
$conn= New-Object System.Data.CData.WordPress.WordPressConnection("Url=http://www.yourwordpresshost.com;InitiateOAuth=GETANDREFRESH;")
$conn.Open()
Instantiate the WordPressDataAdapter, execute an SQL query, and output the results:
$sql="SELECT Id, Name from Categories"
$da= New-Object System.Data.CData.WordPress.WordPressDataAdapter($sql, $conn)
$dt= New-Object System.Data.DataTable
$da.Fill($dt)
$dt.Rows | foreach {
Write-Host $_.id $_.name
}
Update-WordPress -Connection $WordPress -Columns @('Id','Name') -Values @('MyId', 'MyName') -Table Categories -Id "MyId"
$cmd = New-Object System.Data.CData.WordPress.WordPressCommand("UPDATE Categories SET Id='1' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.WordPress.WordPressParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Add-WordPress -Connection $WordPress -Table Categories -Columns @("Id", "Name") -Values @("MyId", "MyName")
$cmd = New-Object System.Data.CData.WordPress.WordPressCommand("INSERT INTO Categories (Id) VALUES (@myId)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.WordPress.WordPressParameter("@myId","1")))
$cmd.ExecuteNonQuery()
Remove-WordPress -Connection $WordPress -Table "Categories" -Id "MyId"
$cmd = New-Object System.Data.CData.WordPress.WordPressCommand("DELETE FROM Categories WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.WordPress.WordPressParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject
Download a free trial of the Wordpress Data Provider to get started:
Download NowLearn more:
👁 Wordpress IconProvides .NET developers with the power to easily connect their Web, Desktop, and Mobile applications to data to Wordpress Pages, Posts, Tags, Users, and more!